问题描述
|
有没有办法对不执行任何其他操作的jQuery对象进行回调。就像是:
$(\"div\",this).do(function(){
$(this).hide();
});
我知道怎么做的唯一方法是:
var obj = $(\"div\",this);
$(obj).hide();
解决方法
您可以使用
each()
[docs]方法
$(\"div\",this).each(function(){
// perform some function on each element in the set
$(this).hide();
});
如果您需要在jQuery对象中的每个元素上运行一些自定义代码,这将很有用。
如果您只需要调用另一个method4ѭ之类的jQuery方法,那么您就不需要.each()
。大多数jQuery方法将自动对集合中的所有元素进行操作。他们称此为“隐式迭代”。
, 听起来您正在尝试写
$(this).find(\"div\").hide();
, 是的,可以,但是您可能正在寻找的是每个功能
$(\'div\',this).each(function(){
//do something with all the divs inside this
});