gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:37:45 25 4
gpt4 key购买 nike

底线:

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

详细信息:

我有一个,每个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com