在下面的示例中,我想为每个 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>
<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>
最佳答案
// 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/