javascript - 获取此 <div> 之外的 <div>

标签 javascript jquery

我想获得第二个 div(总价),而不是第一个 div。

此外,我想在div中添加一个$符号,但是当它试图将数据转换为Int时,显然“$”会导致错误。 我有一个变通方法,但是有没有办法连接 $?

非常感谢。

            <!-- Product #1 -->
            <div class="item">
                <div class="quantity">
                    <button class="plus-btn" type="button" name="button">
                    </button>   
                    <div class="total-price">22</div> <!--I dont want this one -->
                </div>
                <div class="total-price">22</div> <!--I want to access this div -->
            </div>

JQuery

                $('.plus-btn').on('click', function(e) {
                    e.preventDefault();
                    //Increase cost
                    var $this = $(this);
                    var $cost = $this.closest('div').find('div');
                    var costValue = parseInt($cost.html());

                    costValue += costValue;

                    $cost.html(costValue);
                });

最佳答案

$('.plus-btn').on('click', function(e) {
 e.preventDefault();
 var $this = $(this);
 var $cost = $this.closest('.quantity').siblings('.total-price');
 var costValue = parseInt($cost.html());

 costValue += costValue;

  $cost.html(costValue);
 });

关于javascript - 获取此 <div> 之外的 <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58195107/

相关文章:

javascript - JQuery Mobile 复选框中的中心文本

javascript - 以调试器的方式在 HTML 中显示 JS 对象的库

javascript - Ajax 成功与错误

javascript - 带有上一个下一个部分图像的全宽轮播 slider

javascript - 将 <a> 标签包裹在 http 文本周围

javascript - Phonegap 转换 jquerymobile

javascript - var $ = this.jQuery 无法理解

javascript - 从助手获取模板数据

javascript 存储很多对象的信息

javascript - 使用 Tab 键在对话框元素内循环在 Safari 和 Chrome 中不起作用