2010-01-18 96 views
1

用下面的例子,我想選擇每個塊的< h3>標籤的內容,並將它們移到'title'div(不帶h3標籤)中。這可能嗎?如何使用jQuery將相同的元素從一個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> 

回答

2

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

首先,我想一個類分配給「塊」,我們稱之爲「塊」現在。然後執行此操作:

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