2011-02-28 2 views
0

다음 코드를 가지고 있습니다.jQuery를 사용하여 메모리 내의 DOM 노드를 대체하는 방법

$.getJSON("../services/getNavigationAffectedList.php", obj, function(response) 
{ 
    $.each(response, function(i, v) 
    { 
     // Get the old page source 
     var source = v; 

     // Get the updated preview HTML 
     var new_nav = $('#preview').find('#navigation-block').html(); 

     // Replace the old navigation with the preview HTML 
     $(source).find('.navigation-block').html(new_nav); 
     var source = $(source).html(); 

     var obj = { 
      page_path: i, 
      new_source: source 
     }; 
     console.log(obj); 
    }); 

어떤 것은 메모리 (소스)에 포함 된 DOM 내에서 탐색 블록 클래스를 대체하려고 시도하고 있습니다. $ (source) .html()이 예상 된 결과를 가져 오지는 않지만.

도움이 되셨습니까?

David의 요청에 따라 부분적인 JSON 응답 :

{ "var/www// html/cms/site/약 about//debbie.php": "\ r \ n ... 더 많은 HTML을 여기에 .... ....

다음은 테스트 할 바이올린입니다. http://jsfiddle.net/eZP9E/1/

+0

이 문제를 보여줍니다 JSON의 작은 예, 또는 (더 나은) 링크를 포함 할 수 :

문제에 대한 빠른 해결책은 다음과 같이에 코드를 업데이트하는 것? http://jsfiddle.net은 라이브 링크가없는 경우 훌륭한 도구입니다. –

+0

{\\ var \/www \/html \/cms \/sites \/about \ /debbie.php ":" \ r \ n ... ....} –

+0

@ syn4k : 죄송합니다. 마지막 코멘트는 무엇 이었습니까? ** 덧글 **. ** 질문 **에 추가 세부 정보 **를 편집하십시오. –

답변

0

는 jQuery의 replaceWith 기능을 사용해보십시오 당신이

이 찾고있는 무엇

http://api.jquery.com/replaceWith/

원하지 않는 결과는 무엇입니까?

업데이트 : 문제는 jQuery에서 DOM 요소를 만드는 방식과 관련이 있습니다.

코드에 'var original'이 (가) 있지만 요소는 아닙니다.

var container = $('<html><body><div class="navigation">1</div></body></html>'); 
alert(container.length); // returns 1, div.navigation 

var container = $('<html><body><div class="navigation">1</div><div class="body"></div></body></html>'); 
alert(container.length); // returns 2, div.navigation, div.body 

var container = $('<div id="root"><html><body><div class="navigation">1</div></body></html></div>'); 
alert(container.length); // returns 1, sweet! we can use this 

일부 테스트를 마친 결과 위 코드에서 얻은 결과입니다.

var original = $('<div id="root">' + v + '</div>', document); 
+0

흥미롭게도 , replaceWith 만 보이는 것 같습니다. o 스니 j에서와 같이 메모리 DOM 대신 페이지 DOM을 사용하십시오. –

+0

다음은 함께 테스트 할 수있는 피들입니다. http://jsfiddle.net/eZP9E/1/ –

+0

@ syn4k, 답변을 업데이트했습니다. 나는 당신의 바이올린에 대한 단일 업데이트를 만들었고 매력처럼 작동했습니다 : http://jsfiddle.net/eZP9E/3/ – McHerbie

관련 문제