2009-10-23 4 views
5

페이지에서 래핑 노드 요소를 이동해야하는 스크립트를 작성하고 있습니다. 나는 이것을 할 때 이전에 감싸 인 어린이들을 제거합니다. 노드의 하위 노드를 중첩 해제하지 않으므로 상위 노드를 다른 위치로 이동할 수 있습니까?javascript에서 "surroundContents"를 어떻게 취소합니까?

나는 이런 식으로 뭔가를 생각하고 있었다 :

var parg = document.getElementById("blah"); 

    if (parg.hasChildNodes()) 
    { 
    var children = parg.childNodes; 
    while (children.length > 0) 
    { 
     parg.insertBefore(parg.firstChild); 
     parg.removeChild(parg.firstChild); 
    }; 
    }; 

내가 추측하고있어 라인을하는 문제가된다 "에 insertbefore"논리.

+0

위대한 질문! 정말로 문제를 명확하게 표현하십시오. – toddmo

답변

7

에 insertbefore는 요소 노드에서 작동하며 두 개의 인수, 새로운 노드, 새로운 노드가 선행 될 노드를합니다.

function unwrap(who){ 
var pa= who.parentNode; 
while(who.firstChild){ 
    pa.insertBefore(who.firstChild, who); 
} 
} 

// 테스트

언랩 (document.getElementById를 ("ㅋ"));

enter image description here

+0

이것은 환상적인 해결책입니다. 감사합니다. – Matrym

0

첫 번째 레벨 하위를 반복하고 부모를 "래퍼"요소의 상위에 할당해야합니다. 아마도 이런

뭔가 :

if (parg.hasChildNodes()) { 
    var children = parg.childNodes; 

    for (child in children) { 
     child.parentNode = parg.parentNode; 
    } 
}