[英]JQuery data html attribute not returning value
这是我的html:
<ul class="inboxList">
<li data-selecttype='global|mailbox|inbox'>Inbox</li>
</ul>
这是我的jquery代码:
$(".inboxList").children().each(function(child){
console.log($(child).data("selecttype"));
});
我也尝试取出孩子的$():
$(".inboxList").children().each(function(child){
console.log(child.data("selecttype"));
});
但这没有用。
我的返回值为null且未定义。 我期望返回值是global|mailbox|inbox
我做错了什么?
感谢您的协助!
您在每个的回调中使用了错误的参数。 您应该为元素使用第二个arg或抛弃args并仅使用this
。 它应该是:
$(".inboxList").children().each(function(i, child){ // each has 2 args first one is index and second one is the element.
console.log($(child).data("selecttype")); //Or this
console.log($(this).data('selecttype'));
});
.each(function(index(Element,Element))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.