首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按数据属性获取元素id

按数据属性获取元素id
EN

Stack Overflow用户
提问于 2016-06-07 11:21:27
回答 4查看 300关注 0票数 1

我有以下html输出:

代码语言:javascript
复制
...
<ul id="commentlist">
    <li id="comment-12" data-post="post-1">Comment 1 for post 1</li>
    <li id="comment-34" data-post="post-1">Comment 2 for post 1</li>
    <li id="comment-56" data-post="post-1">Comment 3 for post 1</li>
</ul>
<ul id="commentlist">
    <li id="comment-78" data-post="post-2">Comment 1 for post 2</li>
    <li id="comment-90" data-post="post-2">Comment 2 for post 2</li>
</ul>
...

请帮助我用jQuery生成2个维数组(如下所示):

代码语言:javascript
复制
array = [
    "post-1": ["comment-12", "comment-34", "comment-56"],
    "post-2": ["comment-78", "comment-90"] 
]

我试过:

代码语言:javascript
复制
jQuery("li[data-post]").each(function(){
    /*console.log(jQuery(this)); -- this contains necessary "id" but I don't know how to fetch it*/

    var testdata = jQuery(this).data('post');
    if (comment_lists.indexOf(testdata) == -1)
        comment_lists.push(testdata);
});
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-06-07 11:30:36

使用each()方法对元素进行迭代,并根据数据属性在内部生成对象。

代码语言:javascript
复制
var obj = {};

// select elements with the attribute and iterate
$('[data-post]').each(function() {
  // get data attribute value
  var d = $(this).data('post');
  // define the property if not already defined
  obj[d] = obj[d] || [];
  // push the id of element
  obj[d].push(this.id)
})

console.log(obj)
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="commentlist">
  <li id="comment-12" data-post="post-1">Comment 1 for post 1</li>
  <li id="comment-34" data-post="post-1">Comment 2 for post 1</li>
  <li id="comment-56" data-post="post-1">Comment 3 for post 1</li>
</ul>
<ul id="commentlist">
  <li id="comment-78" data-post="post-2">Comment 1 for post 2</li>
  <li id="comment-90" data-post="post-2">Comment 2 for post 2</li>
</ul>

票数 3
EN

Stack Overflow用户

发布于 2016-06-07 11:26:05

您可以使用对象代替数组来返回如下所示的数据

代码语言:javascript
复制
var obj = {};

$('ul').each(function() {
  var li = $(this).find('li');
  obj[li.data('post')] = li.map(function() {return $(this).attr('id') }).get();
})

console.log(obj)
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="commentlist">
  <li id="comment-12" data-post="post-1">Comment 1 for post 1</li>
  <li id="comment-34" data-post="post-1">Comment 2 for post 1</li>
  <li id="comment-56" data-post="post-1">Comment 3 for post 1</li>
</ul>
<ul id="commentlist">
  <li id="comment-78" data-post="post-2">Comment 1 for post 2</li>
  <li id="comment-90" data-post="post-2">Comment 2 for post 2</li>
</ul>

票数 1
EN

Stack Overflow用户

发布于 2016-06-07 11:30:01

最简单的解决方案是迭代所有[data-post]元素并将它们写入对象。

var数组= {};

代码语言:javascript
复制
$('[data-post]').each(function(idx, el){
  var name = $(el).attr('data-post');
  if (!array.hasOwnProperty(name) {
    array[name] = [];
  }

  array[name].push($(el).attr('id'));
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37677991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档