0

这是我的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"));
});

但这没有用。

我的返回值为空且未定义。我期望返回值global|mailbox|inbox是我做错了什么?

感谢您的帮助!

4

2 回答 2

3

You are using the wrong argument from each's callback. You should use the second arg for the element or ditch the args and just use this. it should be:

$(".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'));
});

http://jsfiddle.net/JUyHg/

.each( function(index, Element) )

于 2013-06-19T18:58:14.973 回答
0

尝试这个,

$(".inboxList").children().each(function(){
    console.log($(this).data("selecttype"));
});

根据 jQuery 网站上的定义, .each() 接受 2 个参数,index并且Element,其中,

index - 是 .each() 返回的 jQuery 对象中当前匹配元素的索引

Element - 在您的情况下是当前元素

<li data-selecttype=​"global|mailbox|inbox">​Inbox​&lt;/li>​

阅读更多关于.each()

小提琴

于 2013-06-19T19:07:16.463 回答