2010-01-18 3 views
1

, 나는 내용을 각 블록에 대한 < H3> 태그의를 선택하고합니다 (H3 태그 없음) '제목'div의 내부로 이동하고 싶습니다. 이것이 가능한가?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()); 
} 
관련 문제