Generic jQuery callback?
Is there a way to have a callback on a jQuery object that doesn't do anything else. something like:
$("div", this).do(function(){
$(this).hide();
});
The only way I 开发者_运维百科know how to do that is:
var obj = $("div", this);
$(obj).hide();
You can use the each()
[docs] method
$("div", this).each(function(){
// perform some function on each element in the set
$(this).hide();
});
This is useful if you need to run some custom code on each element in the jQuery object.
If all you need is to call another jQuery method like .hide()
, then you don't need .each()
. Most jQuery methods will operate on all elements in the set automatically. They call this "implicit iteration".
It sounds like you're trying to write
$(this).find("div").hide();
Yes you can, but what you're probably looking for is the each function
$('div', this).each(function(){
//do something with all the divs inside this
});
精彩评论