javascript - 检索多个数据属性并设置为子元素的文本

标签 javascript jquery custom-data-attribute jquery-data

底线:

我在检索多个数据属性的值并将它们作为子元素的文本插入时遇到问题。

详细信息:

我有一个,每个tr“桶”包含特定数量的三种形状(球体、立方体、金字塔),它们存储在数据属性。我有一个 ul,每个形状都有相应的 li。当一个形状被选中时,具有超过 0 个该形状的 tr 会被突出显示。 但是,我还尝试检索每个选定形状的编号,并将其显示在每个 trtd.contents 元素中(即“金字塔:300 , 立方体:50"),这是我一直没有成功的地方。

这是 HTML -

<ul>
    <li data-shape="spheres">spheres</li>
    <li data-shape="cubes">cubes</li>
    <li data-shape="pyramids">pyramids</li>
</ul>

<table>
    <tr data-spheres="380" data-cubes="0" data-pyramids="200"><td>bucket 1</td><td class="contents">no shapes selected</td></tr>
    <tr data-spheres="0" data-cubes="90" data-pyramids="0"><td>bucket 2</td><td class="contents">no shapes selected</td></tr>
    <tr data-spheres="100" data-cubes="20" data-pyramids="400"><td>bucket 3</td><td class="contents">no shapes selected</td></tr>
    <tr data-spheres="500" data-cubes="110" data-pyramids="0"><td>bucket 4</td><td class="contents">no shapes selected</td></tr>
</table>

和 JS(第 22 行是我遇到麻烦的地方)-

(function() {
//store tr elements
var buckets = $('tr');

  $('li').click(function () {
    //toggle checked class OK
    $(this).toggleClass('checked');
    //reset classes and .contents text OK
    $(buckets).removeClass('active');
    $('td.contents').text('no shapes selected');
    //map the shape data attribute for all li elements 
    //with class '.checked' OK
    var checkedShapes = $('.checked').map(function() {
        return $(this).data('shape');
    });
    //for each checked shape, filter tr elements with 
    //more than 0 of that shape OK 
    $.each(checkedShapes, function( index, value ) {
      var filteredBuckets = $(buckets).filter(function() {
        return $(this).data(value) > "0";
      });
      //add .active class to those tr elements OK
      $(filteredBuckets).addClass('active');
      //get number of checked shapes (i.e. "pyramids: 300, cubes: 50") 
      //and display in td.contents DOESN'T WORK
      $('tr.active td.contents').text($(this).parent('tr').data(value));
    });
  });

})();

还有一个 jsFiddle:http://jsfiddle.net/a8gtrn63/33/

根据我在 .data() 文档中的理解,这应该是检索选定的数据属性,但事实并非如此。我认为 value 引用可能存在问题,但我看不到它。非常感谢任何输入。

最佳答案

$(this) 指的是 $.each 循环而不是文本,所以我通过遍历元素修复它,然后使用 $(this):

(function() {
//store tr elements
var buckets = $('tr');

  $('li').click(function () {
    //toggle checked class OK
    $(this).toggleClass('checked');
    //reset classes and .contents text OK
    $(buckets).removeClass('active');
    $('td.contents').text('no shapes selected');
    //map the shape data attribute for all li elements 
    //with class '.checked' OK
    var checkedShapes = $('.checked').map(function() {
        return $(this).data('shape');
    });
    //for each checked shape, filter tr elements with 
    //more than 0 of that shape OK 
    $.each(checkedShapes, function( index, value ) {
      var filteredBuckets = $(buckets).filter(function() {
        return $(this).data(value) > "0";
      });
      //add .active class to those tr elements OK
      $(filteredBuckets).addClass('active');
      //get number of checked shapes (i.e. "pyramids: 300, cubes: 50") 
      //and display in td.contents DOESN'T WORK
      $('tr.active td.contents').each(function(){
         $(this).text($(this).parent('tr').data(value));
      });
    });
  });

})();

关于javascript - 检索多个数据属性并设置为子元素的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25389809/

相关文章:

javascript - 在 JQuery 中使用复选框进行过滤时,如何在数据属性的完整字符串中进行搜索,而不仅仅是精确匹配?

jquery - 如何使用自己的数据属性设置CSS属性?

javascript - 如何在 NodeJS 中导入匿名闭包

javascript - 试图将字符串数组中的第一个字符大写,为什么这不起作用?

javascript - CORS 响应 header 如何阻止浏览器 JavaScript 访问响应对象?

javascript - jQuery - 动画 + 滚动顶部

javascript - 同位素中的动画项目大小 : Why is first item wonky?

javascript - Turn.js 的鼠标单击和鼠标悬停效果不起作用

jquery - CSS 布局/动画的 Chrome/jQuery 错误?解决方法?

jquery - 如何从一系列数据属性的元素更改 css?