【问题标题】:How can I get a div's children id using Jquery? [duplicate]如何使用 Jquery 获取 div 的子 ID? [复制]
【发布时间】:2019-03-24 09:07:53
【问题描述】:

我有类似的代码

 var i = $(this).children().eq(0).attr('id');

我想知道,我是否已经将该子 div 的 id 存储在名为 i 的变量中。 还是我必须写一些其他代码?如果我必须编写一些其他代码,它可能是什么?由于我是 jQuery 新手,如果我得到任何建议,将会非常有帮助。

【问题讨论】:

标签: jquery attributes


【解决方案1】:

你应该了解 jQuery 中的 'each' 函数在迭代孩子时,它非常有用。

试试这个:

var parent = $(this); // selector could be anything, like $('#theParentElement')
var childIDs = [];

parent.children().each(function() {

    var child = $(this);
    childIDs.push(child.attr('id'));

    // you can simplify to childIDs.push($(this).attr('id'));
});

【讨论】:

  • 谢谢汤姆_B。你能告诉我,在我的代码中,我是否已经将孩子的 id 存储在 var i 中?还是尚未存储?
  • 乐于助人。根据我的测试,您只会存储第一个子元素的 ID。如果我的解决方案已经回答了您的问题,请您将其标记为正确
猜你喜欢
  • 2014-07-03
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多