2014-03-28 5 views
0

나는 변수에 복사하는 dom이 있습니다. 변수의 특정 요소를 다른 요소로 바꾸고 싶습니다.변수에서 요소를 변경하십시오

<div id="parent"> 
    <section id="1"></section> 
    <section id="2"></section> 
    <section id="3"></section> 
    <section id="4"></section> 
</div> 

이것은 변수에 복사했습니다. 이제 DOM

와 아무 관계가 나는이 내가

var target = document.getElementById('parent'); 
    var wrap = document.createElement('div'); 
    wrap = wrap.appendChild(target.cloneNode(true)); 
+0

가 교체하거나 복사하여 새 DOM으로 붙여 넣을 DOM을 변경하지 않고 .... 마치 마법처럼 작동? –

+0

DOM 조작이 필요 없다. DOM을 운반하는 변수를 변경한다. –

+0

DOM을 변경하지 않으면 DOM 조작이 불가능하기 때문에 DOM 조작이 불가능하다. –

답변

0
var target = document.getElementById('parent'); 
    var wrap = document.createElement('div'); 
    wrap = wrap.appendChild(target.cloneNode(true)); 
    $(wrap).find("#1").html("<img id='5'></img>"); 

0

이 시도하려 무엇

<div id="parent"> 
    <img id="5"></img> 
    <section id="2"></section> 
    <section id="3"></section> 
    <section id="4"></section> 
</div> 

로 변경하고자합니다.

$('#parent').children().each(function(){ 
    var ids = $(this).attr('id'); 
    $(this).replaceWith('<img id="'+ids+'"/>'); 
}); 

Fiddle Demo

+0

dom 조작이 필요 없다. dom을 가지고있는 변수를 바꾼다. –

관련 문제