【问题标题】:JQuery Append remove clone Issue with Multiple Same class divsJQuery Append删除具有多个相同类div的克隆问题
【发布时间】:2017-02-05 10:08:20
【问题描述】:

我遇到了一个问题,我的克隆内容被克隆两次到两组 div。

例如我有以下 html(这个 HTML 不能更改)

<div class="cartItemrowInner col-lg-12">
  <div class="itemTitle col-lg-12"></div>
  <div class="td_replacement col-lg-12">
    <label for="quantity_0" class="aural-only">Quantity </label>
  </div>
  <div class="td_replacement col-lg-12">
    <h5><span class="CartItemName">Pie 1</span></h5>
  </div>
  <div class="td_replacement col-lg-12">
    <p>$25.00</p>
  </div>
</div>

<div class="cartItemrowInner col-lg-12">
  <div class="itemTitle col-lg-12"></div>
  <div class="td_replacement col-lg-12">
    <label for="quantity_0" class="aural-only">Quantity </label>
  </div>
  <div class="td_replacement col-lg-12">
    <h5><span class="CartItemName">Pie 2</span></h5>
  </div>
  <div class="td_replacement col-lg-12">
    <p>$25.00</p>
  </div>
</div>

当我尝试使用附加删除克隆将倒数第二个 .td_replacemnt 内容移动到 .itemTitle 时

它使用这个 jQuery 将两组 .td_replacements 复制到两个 .itemTitles 中。

$('.itemTitle').append( $('.itemTitle').next('div').next('div').remove().clone() );

// THIS COULD NOT FIND CONTENTS OF .TD_replacement
$('.itemTitle').append( function(){

$(this).next('div').next('div').remove().clone() 

});

所以我认为在函数中使用 $(this) 可能会解决问题,但它找不到元素。

非常感谢您的帮助。谢谢你

编辑***

这让我更接近,但它只是将顶部馅饼 1 移动到正确的位置

var itemTitle = $('.itemTitle');
for(var i=0; i<itemTitle.length; i++){
var itemClone = $(itemTitle).next('div').next('div').remove().clone();
$(itemTitle[i]).append(itemClone[i]);
}

【问题讨论】:

    标签: jquery append clone


    【解决方案1】:

    哇!谢谢你的帮助! (不)我不敢相信我没有收到一个答案??那从未发生过。

    反正就是这样!!!对于其他有问题的人!不过,自己解决这个问题确实感觉很好。好的,我已经完成了肥皂盒!

    var itemDescriptions = $('.cartItemrowInner > div:nth-last-child(2)');
    var newTitlelocation = $('.itemTitle');
    
    for(var i=0; i<itemDescriptions.length && i<newTitlelocation.length; i++){
    $(newTitlelocation[i]).append(itemDescriptions[i]);
    
    }
    

    非常感谢 JavaBits https://stackoverflow.com/users/651362/javabits

    致Giving multiple conditions in for loop in Java

    【讨论】:

      猜你喜欢
      • 2012-12-02
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多