这看起来很简单,但我不明白为什么它不起作用.选择器是正确的但是div .faqContent不是用data-height属性更新的.
$('.faqItem .faqContent').each(function(){
var h = $(this).height();
$(this).data('height',h);
});
Run Code Online (Sandbox Code Playgroud)
我检查过这 var h是正确的,它在colsole.log中正确地保持高度.
编辑 绝对没有冲突,控制台显示没有错误.
请考虑以下HTML:
<div class="a" x="6"></div>
<div class="a" x="9"></div>
<div class="a" x="2"></div>
...
<div class="a" x="8"></div>
Run Code Online (Sandbox Code Playgroud)
您如何找到x所有.a元素的最大值?
假设所有x值都是正整数.
可能重复:
按数据属性的数值排序元素
我想根据设置的数据属性重新组织div,范围从最高整数到最低.现在,我将所有数据推送到一个数组并排序,但我不知道如何继续.我如何使用它来重新组织父div中的div?
var prices = new Array();
$(this).children('.listing-item').each(function() {
var price = parseInt($(this).attr('data-listing-price'));
prices.push(price)
});
prices.sort(function(a,b){return b-a});
Run Code Online (Sandbox Code Playgroud)
这让我得到一个像139,129,129,109,109,84这样的数组.现在我的想法是使用这样的选择器再次遍历div:
$('.listing-item[data-listing-price="' + prices[0] + '"]')
Run Code Online (Sandbox Code Playgroud)
但我不确定如何移动div(具有最高数据属性整数的div应该移到顶部等等.有什么想法吗?