jquery - 如何定位 DOM 中的克隆元素 (jQuery)

标签 jquery html

这是我的标记:

<div class="main">
    <div class="box">
        <div class="box_content">
            <div class="box_price">180</div>
            <div class="box_button">Click me</div>
        </div>
    </div>
    <div class="box">
        <div class="box_content">
            <div class="box_price">130</div>
            <div class="box_button">Click me</div>
        </div>
    </div>
    <div class="box">
        <div class="box_content">
            <div class="box_price">270</div>
            <div class="box_button">Click me</div>
        </div>
    </div>
</div>

<div class="container">

</div>

<div class="cost"></div>

所以我使用以下代码在 jQuery 中克隆并附加了一些 .box div 从 .main 到 .container:

$(document).ready(function(){
"use strict";
    $(".box_button").click(function(){

        var box_content = $(this).parents('.box').clone(); //Box is cloned
        var price = $(box_content).find(".box_price");

        $(price).toggleClass('box_price sc_box_price'); //Class changed to sc_box_price
        $(box_content).append('.container'); //Box is appended
    });
});

在附加之前,我将子元素 .box_price 的类更改为 .sc_box_price。

我有另一个代码,它应该计算所有 .sc_box_price 的总和并将其附加到 .cost。

$(document).ready(function(){
"use strict";

    var box_price = $(".sc_box_price");
    var total = 0;

    $(box_price).each(function(){
        total += parseInt($(this).text());
    });
    $('.cost').prepend("<div class='cost'>"+total+"</div>");
    console.log(total);
});

出于某种原因,我无法定位 .sc_box_price。也许是因为 .sc_box_price 并没有真正添加到 DOM 中?我该如何解决这个问题?

最佳答案

而不是 append() 您需要使用 appendTo 来附加您的克隆框。

appendTo 和 append 之间有区别,即

take this new thing and appendTo an already existing thing
vs
take already existing thing and append this new thing

这就是为什么你需要使用 appendTo()

现在,在附加后,将您的其他代码添加到 onClick 事件中,在 appendTo 语句之后 此外,您不必每次都附加一个 div,只需更改 cost div 的文本即可。我想这就是您想要的。

$(document).ready(function(){

    $(".box_button").click(function(){
        
        var box_content = $(this).parents('.box').clone(); //Box is cloned
        var price = $(box_content).find(".box_price");

        $(price).toggleClass('box_price sc_box_price'); //Class changed to sc_box_price
        
        $(box_content).appendTo('.container'); //Box is appended
      
         var box_price = $(".sc_box_price");
          var total = 0;

          $(box_price).each(function(){
              total += parseInt($(this).text());
          });
          
          $('.cost').text(total);
          
          });
    
    
});
.cost {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
    <div class="box">
        <div class="box_content">
            <div class="box_price">180</div>
            <div class="box_button">Click me</div>
        </div>
    </div>
    <div class="box">
        <div class="box_content">
            <div class="box_price">130</div>
            <div class="box_button">Click me</div>
        </div>
    </div>
    <div class="box">
        <div class="box_content">
            <div class="box_price">270</div>
            <div class="box_button">Click me</div>
        </div>
    </div>
  
   
</div>

<div class="container">

</div>

<div class="cost"></div>

关于jquery - 如何定位 DOM 中的克隆元素 (jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39917154/

相关文章:

点击后Javascript延迟,设置超时不起作用

javascript - 重新加载页面时随机化内容不循环

javascript - 创建带有可滚动图片的 div 容器时出现问题

php - HTML 缩进不适用于 PHP include

html - 关于 margin property 和 value auto

javascript - 在 javascript 中返回数组和子数组

jquery - 在下拉菜单中显示 ID

javascript - 如何从输入类型文件生成缩略图和文件名并将它们插入到多个元素中?

javascript - Angular : Uncaught Error: [$injector:modulerr] when using updated version of angularJS

html - 更改 Accordion 显示颜色