javascript - 如何使用 jQuery 将相同的元素从一个 div 移动到另一个?

标签 javascript jquery html

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

标签的内容,并将它们移动到“标题”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>

最佳答案

在线演示: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());
});

关于javascript - 如何使用 jQuery 将相同的元素从一个 div 移动到另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2089426/

相关文章:

javascript - 向日期添加天数并显示它,不包括周末和节假日

javascript - 单击转到带有 id 引用解决方案的动态 url

html - 图像未显示在输入字段内

html - 如何跟踪当前选择了哪个图标而不是一次选择多个图标

javascript - Phonegap index.js 未输入 $(function(){})

javascript - Fabricjs在 Canvas 中复制html

javascript - jquery .parent() 选择错误的元素

javascript - 使用jquery更改父div中的元素位置

javascript - 返回数据过滤器 onclick 可能吗?

javascript - 转到 Typescript Source Definition 而不是 Visual Studio Code 中的 Compiled Definition