所以我有以下 HTML:
<div class="calculator-section">
<p>
<span class="x"></span>
<span class="amount">30</span>
</p>
<p>
<span class="y"></span>
</p>
</div>
我想做的是获取 <span>
中的数字每个 calculator-section
中都有“数量”类基于类“x”的跨度。类 'calculator-section' 和多个 <p>
可以有多个 div。在具有不同跨度的那些中。
大致是我想做的(我知道这行不通,只是为了展示我想做的):
var amount = 0;
$('.calculator-section p').each(function(i, obj) {
if($(this).$('span').className == "x") {
//Set 'amount' variable to value within <span> with class 'amount' within the same <p>.
}
});
希望这个例子有意义。有什么想法吗?
最佳答案
我建议:
$('.calculator-section p .x').text(function () {
return $(this).next('.amount');
});
引用资料:
关于javascript - 根据 div 中的其他 <span> 类获取 <span> 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26076382/