jQuery每个错误:未捕获TypeError:不能使用'in'运算符在div中搜索'18'[data-role = page] [英] jQuery each error :Uncaught TypeError: Cannot use 'in' operator to search for '18' in div[data-role=page]
本文介绍了jQuery每个错误:未捕获TypeError:不能使用'in'运算符在div中搜索'18'[data-role = page]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的html就像
< div data-role =pageid =Dialog_1data-url = Dialog_1 > ...< / DIV>
< div data-role =pageid =Dialog_2data-url =Dialog_2> ...< / div>
。
。
。
< div data-role =pageid =Dialog_17data-url =Dialog_17> ...< / div>
< div data-role =pageid =Dialog_18data-url =Dialog_18> ...< / div>
我想通过$ .each
选择所有data-role =page我的jQuery
$。each(div [data-role = page] ,function(){
console.log($(this).attr('id'));
});
它给出错误:
未捕获的TypeError:无法使用'in'运算符在div中搜索'18'[data-role = page]
解决方案
您需要提供一个jQuery集合,而不仅仅是 $。each()
:
$。each($(div [data-role = page]),function(){
console.log(this.id);
});
或者,甚至更好:
<$ p $ ()函数(){
console.log(this.id);
});
请注意我替换了 $(this).attr('id' )
与 this.id
。它获得完全相同的属性,但效率更高。
My html is like
<div data-role="page" id="Dialog_1" data-url="Dialog_1">...</div>
<div data-role="page" id="Dialog_2" data-url="Dialog_2">...</div>
.
.
.
<div data-role="page" id="Dialog_17" data-url="Dialog_17">...</div>
<div data-role="page" id="Dialog_18" data-url="Dialog_18">...</div>
i want to select all data-role="page" by the $.each
My jQuery
$.each("div[data-role=page]", function (){
console.log($(this).attr('id'));
});
it is giving the error:
Uncaught TypeError: Cannot use 'in' operator to search for '18' in div[data-role=page]
解决方案
You need to supply a jQuery collection, instead of just a selector to the $.each()
:
$.each($("div[data-role=page]"), function (){
console.log(this.id);
});
Or, even better:
$("div[data-role=page]").each(function (){
console.log(this.id);
});
Please note that I replaced $(this).attr('id')
with this.id
. It gets exactly the same property, but it's way more efficient.
这篇关于jQuery每个错误:未捕获TypeError:不能使用'in'运算符在div中搜索'18'[data-role = page]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文