/ / गतिशील रूप से बनाए गए तत्वों के लिए jQueryUI Resizable विजेट लागू करें - jquery, jquery-ui, jquery-ui-resizable

गतिशील रूप से बनाए गए तत्वों - jquery, jquery-ui, jquery-ui-resizable

मैं की एक श्रृंखला के लिए jQuery के resizable विजेट लागू करना चाहते हैं div संभावित रूप से बनाए जा सकने वाले टैग।

क्या किसी चीज़ का उपयोग करने का एक तरीका है बर्ताव करती है गतिशील रूप से jQuery जोड़ने के लिए इवेंट डेलिगेशन की तरह resizable नए तत्वों के लिए विजेट?

यहां उदाहरण है (ध्यान दें कि नए बनाए गए हरे तत्वों का आकार कैसे बदला नहीं जा सकता है):

$(function() {
var cols = $("#cols").children(".col");

cols.resizable({
maxHeight: 20,
minHeight: 20,
distance: 5,
handles: "e",
start: function() {
console.log("I"ve started!");
},
stop: function() {
console.log("I"ve stopped!");
}
});

$("#addNew").on("click", function() {

cols.filter(":last").after("<div class="col new">" + parseInt(cols.length + 1) + "</div>");

cols = $("#cols").children(".col");

});
});
.col {
float: left;
width: 20px;
height: 20px;
background: #f00;
margin-right: 1px;
}
.col.new {
background: #0f0;
}
button {
clear: left;
display: block;
margin-top: 10px;
}
<link href="https://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<div id="cols">
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>
</div>
<button id="addNew">Add</button>

संपादित करें: मैं इसके बारे में नहीं पूछ रहा हूं घटना प्रतिनिधिमंडल, मैं पूछ रहा हूं कि क्या रिज्यूम करने योग्य विजेट को डायनामिक रूप से नए बनाए गए तत्वों में जोड़ने का कोई तरीका है।

उत्तर:

जवाब के लिए 2 № 1

आपको पुन: स्थापित करने की आवश्यकता है col.resizable(); एक बार नया तत्व जोड़ा गया। कृपया कार्य कोड नीचे देखें:

$(function() {
var cols = $(".col");


cols.resizable({
maxHeight: 20,
minHeight: 20,
distance: 5,
handles: "e",
start: function() {
console.log("I"ve started!");
},
stop: function() {
console.log("I"ve stopped!");
}


});


$("#addNew").on("click", function() {

cols.filter(":last").after("<div class="col new">" + parseInt(cols.length + 1) + "</div>");

cols = $(".col");
cols.resizable({maxHeight: 20,
minHeight: 20,
distance: 5,
handles: "e"});

});

});
.col {
float: left;
width: 20px;
height: 20px;
background: #f00;
margin-right: 1px;
}
.col.new {
background: #0f0;
}
button {
clear: left;
display: block;
margin-top: 10px;
}
<link href="https://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

<div id="cols">

<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>

</div>
<button id="addNew">Add</button>


उत्तर № 2 के लिए 1

मुझे पता है कि यह एक पुरानी पोस्ट है, लेकिन फिर भी मुझे लगा कि मैं हूंभविष्य के संदर्भ के लिए एक बेहतर उत्तर पोस्ट कर सकता है, क्योंकि आपकी टिप्पणियों में आपने कोड के पुन: उपयोग के बारे में बताया था और कई बार तत्वों को प्लगइन लागू नहीं करने पर यह समाधान उन मुद्दों को दूर करना चाहिए।

$(function() {

ApplyResizablePluginToCol($(".col"));  // cal the function by passing all the elements to which the plugin must be applied to

$("#addNew").on("click", function() {

$(".col:last").after("<div class="col new">" + parseInt( $(".col").length + 1) + "</div>");
ApplyResizablePluginToCol($(".col:last"));// take the last element, ie the element that is created just now

});

function ApplyResizablePluginToCol(elements){ //reusable function

elements.resizable({
maxHeight: 20,
minHeight: 20,
distance: 5,
handles: "e",
start: function() {
console.log("I"ve started!");
},
stop: function() {
console.log("I"ve stopped!");
}
});
}

});
.col {
float: left;
width: 20px;
height: 20px;
background: #f00;
margin-right: 1px;
}
.col.new {
background: #0f0;
}
button {
clear: left;
display: block;
margin-top: 10px;
}
<link href="https://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

<div id="cols">

<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>

</div>
<button id="addNew">Add</button>


जवाब के लिए 0 № 3

इस कोड की कोशिश करो यह मेरे लिए काम कर रहा है

$("#cols").on("mouseenter",cols,function(){
cols.resizable({
maxHeight: 20,
minHeight: 20,
distance: 5,
handles: "e",
start: function() {
console.log("I"ve started!");
},
stop: function() {
console.log("I"ve stopped!");
}

});

});