2012-06-02 5 views
3

jquery를 통해 동적 HTML을 만들고, 관심있는 부분에 필터를 적용하고, html의 해당 섹션을 페이지의 기존 요소에 추가하려고하지만 작동하지 않습니다. 이 바이올린에서 내가 뭘 잘못하고 있니?jquery로 다이내믹 html 필터링하기

http://jsfiddle.net/3zKeL/4/

HTML :

var response = "<html><body><h1>hello</h1></body></html>"; 
$(response).filter("body").appendTo("#output"); 
+0

당신이 경우에

http://jsfiddle.net/pqyeM/13/'을 console.log ($ (응답))'는'$를 사용할 수 있도록, 당신은 [

안녕하세요

]''단지 볼 (응답) .appendTo ("# output"); ' –

답변

0

$('<div/>')   // add response to fake div 
    .append(response) 
    .find('body')  // find your target 
    .appendTo('#output'); // append the target 
16,이 알아 냈어. jQuery는 동적으로 생성 된 HTML에서 "html"및 "body"태그를 발견하면 자동으로 구토합니다. 나는 그 태그들을 대체하거나 제거해야만했으며 이제 예상대로 작동한다.

var response = "<html><head><title>test</title><style>body{font-size:.9em;}</style></head><body bgcolor=\"white\"><h1>hello</h1></body></html>"; 

// we have to remove/replace certain tags or jquery will vomit with unexpected results 
var modifiedResponse = response.replace("<html>", "").replace("</html>", "").replace("<body", "<div id='content'").replace("</body>", "</div>"); 

var wrappedSet = $(modifiedResponse); 

wrappedSet.filter("div[id='content']").appendTo("#output"); 
3
$(response) 
      .filter(function() { 
      return $("body"); 
      }) 
      .appendTo("#output"); 

DEMO

당신은

도 수행 할 수 있습니다

<div id="output">This is the output:<br></div> 

JQ 좋아

DEMO

+0

내가 있었다 너무 빨리 받아 들여. 필터는 body 태그의 내용뿐만 아니라 head 태그의 모든 제목 및 스타일 요소를 반환합니다. http://jsfiddle.net/dcwNJ/ – TugboatCaptain