jQuery 如何在Jquery的div中获取div id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1453504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:29:22  来源:igfitidea点击:

How to get the div id's in a div in Jquery?

jquery

提问by Martin Ongtangco

How do you get the id's in a div?

你如何在div中获取id?

<div id="container">
   <div id="frag-123">ass</div>
   <div id="frag-123">ass</div>
   <div id="frag-123">ass</div>
</div>

Thanks!

谢谢!

回答by cletus

There are multiple ways to do this. You can use map():

有多种方法可以做到这一点。您可以使用map()

var ids = $("#container").children().map(function(n, i) {
  return n.id;
});

or each():

each()

$("#container").children().each(function(n, i) {
  var id = this.id;
  // do something with it
});

etc

等等

回答by middus

You can use

您可以使用

attr('id')

回答by knoopx

$('div', $('div#container')).each(function() {
    console.log($(this).attr('id')); 
});

回答by Russ Cam

to get them as an array of strings

将它们作为字符串数组获取

var ids = $.map($('#container div'), function(n,i) {
              return n.id
          });