简体   繁体   English

获取<span>基于<span>div中</span>其他<span>类的</span></span>值<span><span>?</span></span>

[英]Get value of <span> based on other <span> class within div?

So I have the following HTML: 所以我有以下HTML:

<div class="calculator-section">
    <p>
        <span class="x"></span>
        <span class="amount">30</span>
    </p>
    <p>
        <span class="y"></span>
    </p>
</div>

What I want to do is get the number inside the <span> with class 'amount' within each calculator-section based on the span with class 'x'. 我想要做的是基于类“ x”的跨度,在每个calculator-section获取类“金额”在<span>的数字。 There can be multiple divs with the class 'calculator-section' and multiple <p> within those with varying spans. 可以有多个div类别为“ calculator-section”的类,而多个<p>跨度不同。

Roughly what I want to do (I'm aware this won't work, it's just to show what I'm trying to do): 大概是我想做的(我知道这是行不通的,只是为了展示我想做的事情):

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>.
    }
});

Hopefully this example makes sense. 希望这个例子有意义。 Any ideas? 有任何想法吗?

I'd suggest: 我建议:

$('.calculator-section p .x').text(function () {
    return $(this).next('.amount');
});

References: 参考文献:

Try this 尝试这个

var amount = 0;
$('.calculator-section p span').each(function(i, obj) {
    if($(this).attr('class') == "x") {
        amount = $(this).next('.amount').text();
        return false;
    }
});
alert(amount);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM