<div id="cardSlots">
<div class="ui-droppable" tabindex="-1" id="card1">one</div>
<div class="ui-droppable" tabindex="-1" id="card2">two</div>
<div class="ui-droppable" tabindex="-1">three</div>
<div class="ui-droppable" tabindex="-1">four</div>
</div>
<script>
$(".ui-droppable").each(function () {
if($(this).attr("id").length>0)
{
alert('here');
}
});
</script>
我正在尝试遍历类,但问题是我在该页面中有重复的card1 和card2 id。但上面的代码似乎可以工作,但显示以下错误。
Uncaught Type Error: Cannot read property 'length' of undefined
我正在尝试从那里的循环中获取 id。
Use 属性选择器 selector[attribute]
仅获取具有 ID 的元素
$('.myClass[id]') // Get .myClass elements that have ID attribute
在你的情况下:
$('.ui-droppable[id]').each(function(){
alert( this.id );
});
jsFiddle 演示
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)