Mi chiedevo perché in così tanti plugin jQuery $ (questo) è impostato per indicare $ this, ecco un esempio, se ho i seguenti due plugin inclusi una pagina:
(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)
quando chiamo entrambi i plug-in pronto dom:
$(document).ready({
$('.myClass').pluginOne();
$('.myOtherClass').pluginTwo();
});
il primo plugin otterrà $ questo dal secondo plug-in ... mentre io punto $ (this) ad un var locale:
(function($) {
jQuery.fn.pluginTwo = function() {
return this.each(function() {
var trigger = $(this); <--
alert(trigger);
});
};
})(jQuery)
dovrebbe funzionare, naturalmente ...
Quindi la mia domanda è ... quando dovrei usare $ questo?
Grazie
Giusto per chiarire ... Nell'esempio di cui sopra $ (' myOtherClass.) PluginTwo().; restituirà il primo elemento dell'array seguente: $ ('. myClass') –