我想知道为什么像在jQuery.each()中那样在jQuery.first()中包装一个函数()不起作用?
虽然这可以正常工作:
$(this).each(function(){something});
这不起作用:
$(this).first(function(){something});
我真的必须使用此变通方法吗(?) :
$(this).first().each(function(){something});
first()给出了元素本身。
ele = $(this).first();
然后您可以对元素执行您想要执行的任何操作,例如:
if (ele.hasClass("special")){
do whatever.
}
如果想要使用each(),但只对第一个元素做一些事情,可以使用break。 不过,那就太傻了。