如何检查div是否有id? [英] how to check if div has id or not?
本文介绍了如何检查div是否有id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<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重复。但上面的代码似乎工作,但显示下面的错误。
I am trying to loop through class but issue is i have card1 and card2 ids duplicate in that page. but above code seems to work but showing below error.
Uncaught Type Error: Cannot read property 'length' of undefined
我正在尝试从循环中获取ID。
I am trying to get ids from the loop which are there.
推荐答案
使用属性选择器 选择器[attribute]
仅获取具有ID的元素
Use attribute selector selector[attribute]
to get only the elements that have an ID
$('.myClass[id]') // Get .myClass elements that have ID attribute
在您的情况下:
$('.ui-droppable[id]').each(function(){
alert( this.id );
});
这篇关于如何检查div是否有id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文