【问题标题】:How do I move identical elements from one div to another with jQuery?如何使用 jQuery 将相同的元素从一个 div 移动到另一个 div?
【发布时间】:2010-01-18 21:42:44
【问题描述】:

在下面的示例中,我想为每个块选择

标签的 contents,并将它们移动到“标题”div 中(没有 h3 标签)。这可能吗?
<div id="block1">
  <div class="title">
  </div>
  <div class="content">
    <h3>Title 1</h3>
  </div>
</div>

<div id="block2">
  <div class="title">
  </div>
  <div class="content">
    <h3>Title 2</h3>
  </div>
</div>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    在线演示:http://jsbin.com/ezuxo

    // Cycle through each <div class="content"></div>
    $(".content").each(function(){
      // Find its first h3 tag, and remove it
      var heading = $("h3", this).remove();
      // Set the text of the h3 tag to the value of the previous div (.title)
      $(this).prev().html($(heading).html());
    });
    

    【讨论】:

      【解决方案2】:

      首先,我会为“块”分配一个类,我们暂时称它为“块”。然后这样做:

      $(".block").each(function() {
        $(".title", this).html($(".content h3", this).html());
      }
      

      【讨论】:

        猜你喜欢
        • 2011-11-06
        • 2012-06-09
        • 1970-01-01
        • 2014-03-15
        • 1970-01-01
        • 2019-05-19
        • 2013-08-25
        • 1970-01-01
        • 2023-01-28
        相关资源
        最近更新 更多