मैं सोच रहा था कि क्यों इतने सारे jQuery प्लगइन्स $ (यह) इस पर इंगित करने के लिए सेट हैं, यहां एक उदाहरण है, यदि मेरे पास निम्नलिखित दो प्लगइन्स शामिल हैं एक पेज:
(function($) {
jQuery.fn.pluginOne = function() {
return this.each(function() {
$this = $(this); <--
alert($this);
});
};
})(jQuery)
(function($) {
jQuery.fn.pluginTwo = function() {
return this.each(function() {
$this = $(this); <--
alert($this);
});
};
})(jQuery)
जब मैं डोम तैयार पर दोनों प्लगइन्स फोन:
$(document).ready({
$('.myClass').pluginOne();
$('.myOtherClass').pluginTwo();
});
पहले प्लगइन दूसरी प्लगइन से इस $ मिल जाएगा ... जबकि यह मैं एक के लिए $ (this) बात स्थानीय var:
(function($) {
jQuery.fn.pluginTwo = function() {
return this.each(function() {
var trigger = $(this); <--
alert(trigger);
});
};
})(jQuery)
सबकुछ उस पर काम करता है, निश्चित रूप से ...
तो मेरा सवाल है ... मुझे इसका उपयोग कब करना चाहिए?
धन्यवाद
बस स्पष्ट करने के लिए ... ऊपर के उदाहरण $ ('myOtherClass।') में pluginTwo()।; निम्नलिखित सरणी का पहला तत्व वापस कर देगा: $ ('myClass') –