gpt4 book ai didi

javascript - 根据子元素的值对 div 进行排序

转载 作者:行者123 更新时间:2023-11-30 09:35:52 27 4
gpt4 key购买 nike

我想根据 .score 子值的值对 .choice_result div 进行排序。因此,在我下面的 js 中,.choice_result.score 在 100% 以内,应该是第一个 div 而不是最后一个。这是 js:

<div class="poll poll_answered">
<div class="poll_div">
<h1><strong>Percentages</strong></h1>
<div class="poll_results" style="display: block;">

<div class="choice_result" data-choice_percent="0">
<p class="choice_result_text">First<p>

<div class="choice_result_width">
</div>
<span class="score">0%</span>

</div>


<div class="choice_result" data-choice_percent="0">
<p class="choice_result_text">Second<p>

<div class="choice_result_width">
</div>
<span class="score">0%</span>

</div>


<div class="choice_result" data-choice_percent="100">
<p class="choice_result_text">Third<p>

<div class="choice_result_width">
</div>
<span class="score">100%</span>

</div>
</div>
</div>

我该怎么做?

最佳答案

这可能是您正在寻找的:

$('.poll_results.sort-by-score').each(function(){
$(this).html(
$(this).find('.choice_result').sort(function(a, b) {
let dsa = parseInt($(a).find('.score').eq(0).text()),
dsb = parseInt($(b).find('.score').eq(0).text());
return (dsa > dsb ? -1 : (dsa < dsb) ? 1 : 0);
})
);
});

$('.poll_results.sort-by-score').each(function(){
$(this).html(
$(this).find('.choice_result').sort(function(a, b) {
let dsa = parseInt($(a).find('.score').eq(0).text()),
dsb = parseInt($(b).find('.score').eq(0).text());
return (dsa > dsb ? -1 : (dsa < dsb) ? 1 : 0);
})
);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="poll poll_answered">
<div class="poll_div">
<h1><strong>Percentages</strong></h1>
<div class="poll_results sort-by-score" style="display: block;">

<div class="choice_result" data-choice_percent="0">
<p class="choice_result_text">First
<p>
<div class="choice_result_width"></div>
<span class="score">0%</span>
</div>


<div class="choice_result" data-choice_percent="0">
<p class="choice_result_text">Second
<p>
<div class="choice_result_width"></div>
<span class="score">0%</span>

</div>

<div class="choice_result" data-choice_percent="100">
<p class="choice_result_text">Third
<p>

<div class="choice_result_width"></div>
<span class="score">100%</span>

</div>
</div>
</div>
</div>
<div class="poll poll_answered">
<div class="poll_div">
<h1><strong>Not sorted percentages</strong></h1>
<div class="poll_results" style="display: block;">

<div class="choice_result" data-choice_percent="0">
<p class="choice_result_text">First
<p>
<div class="choice_result_width"></div>
<span class="score">0%</span>
</div>


<div class="choice_result" data-choice_percent="0">
<p class="choice_result_text">Second
<p>
<div class="choice_result_width"></div>
<span class="score">0%</span>

</div>

<div class="choice_result" data-choice_percent="100">
<p class="choice_result_text">Third
<p>

<div class="choice_result_width"></div>
<span class="score">100%</span>

</div>
</div>
</div>
</div>

它将根据每个 .poll_results.sort-by 中 .scoretext() 值重新排序 .choice_result -score 页面上的实例。

重要说明:此解决方案假定 .choice_result.poll_results 的唯一子项,如您的问题所示。

关于javascript - 根据子元素的值对 div 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645828/

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