Jquery access to $(this) inside a plugin
Is there any way to get an access to $(this) when the plugin is initialized? for example I have a plugin resizable where I need access to an element that is being resized, so I could use it later in a method that is declared inside a plugin
$('.someClass').resizable({
resize:function(event, ui) {
$(this).doSomeStuff() //how to 开发者_开发知识库get access to $(this)? Where it should be declared?
}
})
I've figured out the solution:
$('.someClass').each(function(){
var me = $(this);
$(me).resizable({
resize: function(event, ui) {
$(me).doSomeStuff();
}
});
});
All that it took just to make sure the plugin is inside of $('').each() method
its a problem of scope, so cache the element in a variable:
var elem = $('.someClass');
elem.resizable({
resize:function(event, ui) {
elem.doSomeStuff() //how to get access to $(this)? Where it should be declared?
}
});
edit
n.b. logging 'event', 'ui' to the console should clarify what those args provide you with, also check the api docs: http://docs.jquery.com/UI/Resizable
$('.someClass').resizable({
resize:function(event, ui) {
console.log(event);
console.log(ui);
}
});
$("textarea").resizable({
stop: function (evt, ui) {
$(this).find(':input').focus();
}
});
精彩评论