开发者

How do I move identical elements from one div to another with jQuery?

With the example below, I'd like to sele开发者_JS百科ct the contents of the < h3 > tags for each block, and move them inside the 'title' divs (without the h3 tags). Is this possible?

<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>


Online demo: 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());
});


First off, I'd assign a class to the "blocks", let's call it "block" for now. Then do this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜