2016-09-16 3 views
-1

HTML을 반환하는 ajax 메서드가 있습니다.ajax 호출에서 찾기 및 바꾸기?

반환 된 HTML에서 특정 div를 데이터로 바꾸고 싶습니다.

replaceWith를 보았습니다.하지만 이것은 dom 객체에서만 작동하고 ajax 호출에서 반환 된 HTML에서는 작동하지 않는 것으로 보입니다.

ajax 호출에서 success 메서드 내에서 특정 div를 어떻게 바꿀 수 있습니까?

$.ajax({ 
    url: '/get-data', 
    dataType: 'html', 
    success: function(html) { 
     //find div with class .test and replace it with <div class"abc"></div> 
+2

'var에 DOM = $ (HTML)를; dom.find ('. test') ...' –

답변

2

당신은이 같은 replaceWith(), 사용할 수 있습니다

$.ajax({ 
    url: '/get-data', 
    dataType: 'html', 
    success: function(html) { 
     var $html = $(html); 
     $html.find('.test').replaceWith('<div class="abc"></div>'); 

     // work with $html as required here... 
    } 
});