2012-12-19 3 views
2

내 프로젝트에서 jQuery를 사용하여 DOM을 조작하고 있습니다. 이 노드를 생성하고 부모 노드에 추가합니다 때문에replaceWith jQuery 메서드로 부모 노드가 null입니다.

<!-- language: lang-js -->  
var template = this._Template.split('{0}'); 
    var content = template[0] + this._Content + template[1]; 
    if (!this._BlockNode) { 
     this._BlockNode = $(content); 
     this._ParentNode.append(this._BlockNode); 
    } 
    else { 
     this._BlockNode.replaceWith(content); 
    } 

모든 것이이 방법의 첫 번째 통화 괜찮 :이처럼 작동 클래스 메소드를 가지고있다. 두 번째 호출 (replaceWith() 메소드 사용)도 정상적으로 작동합니다. 그러나 그 후에 속성 this._BlockNode[0].parentNode은 null입니다. 따라서 세 번째로 호출 할 때 replaceWith().parentNode 속성없이 새로운 속성과 함께 작동하지만이 체크로 인해 노드의 내용을 대체하지 않습니다 : if (!isDisconnected(this[0])) { //line 5910 in jQuery 1.8.3.
어떻게 처리할까요?

답변

3

_BlockNode은 항상 현재 콘텐츠 버전을 가리켜 야합니다.

replaceWith으로 전화를 걸면 DOM 구조가 올바르게 업데이트되지만 개체의 내용은 업데이트되지 않습니다. 원본 _BlockNode은 고아가되어 결국 모든 최신 replaceWith 호출이 해당 노드에서 작동하지만 최신 내용에서는 작동하지 않습니다.

이 시도 :

var template = this._Template.split('{0}'); 
var $content = $(template[0] + this._Content + template[1]); 
if (!this._BlockNode) { 
    this._ParentNode.append($content); 
} else { 
    this._BlockNode.replaceWith($content); 
} 
this._BlockNode = $content; 

_BlockNode보다는 jQuery를 객체에 기본 DOM 요소를 유지하는 것이 바람직 할 수있다.

관련 문제