[英]List of li elements in jquery
我不知道为什么我的代码不起作用。
$("#example").find('LI A').hasClass("sth").each(function(){alert($(this))});
萤火虫 说:
$("#example").find('LI A').hasClass("sth").each 不是 function
这段代码中的问题是each
,因为如果我删除它,它不会给我任何错误。
我需要将“a”元素的成立值传递给数组。
hasClass
function 返回 boolean 而不是 jQuery object。因此它没有each
function。
你可能是这个意思:
$("#example").find('LI A.sth').each(function(){alert($(this))});
或者这个(哪个更好):
$("#example li a.sth").each(function(){alert($(this))});
阅读文档:
.hasClass( className )返回:Boolean
描述:确定是否有任何匹配元素被分配了给定的 class
您对 hasClass 的应用不正确。 它不返回 jQuery Object,而是返回 boolean,因此.each()
不能应用于它。
您必须将 class 附加到选择器
$("#find").click(function (){
$("#example").find('a.sth').each(function(){
$("#test").append($(this));
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.