简体   繁体   English

jQuery:如何获取DID ID?

[英]JQuery: How do I get a DID ID?

I have the following HTML: 我有以下HTML:

<div class="house">...</div>

But in my code I dynamically insert in DIV ID's to make the code then look like this: 但是在我的代码中,我动态地插入了DIV ID以使代码看起来像这样:

<div class="house" id="id_1">...</div>

Question : How can I get the DIV ID by only knowing the CLASS using JQuery? 问题 :如何仅通过使用JQuery知道CLASS来获得DIV ID? I've tried something like the following but it doesn't work. 我已经尝试过类似以下的方法,但是它不起作用。

$('.house').getID();
$('div.house')
  .each(function(index) {
    alert( 'id for this div.class #'+index+': '+$(this).attr('id') );
});

Use the jQuery.attr() method to get and set attributes. 使用jQuery.attr()方法获取和设置属性。

var houseId = $('.house').attr('id');

Note: This will only get the last '.house' element in the DOM's id. 注意:这只会获得DOM ID中的最后一个“ .house”元素。

I believe that 我相信

$('.house').attr("id");

Should work. 应该管用。 I didn't test it though. 我没有测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM