3

jqueryを使用してダイナミックHTMLを作成し、関心のある部分にフィルタリングして、HTMLのそのセクションをページ上の既存の要素に追加しようとしていますが、機能していません。このフィドルで私は何を間違っているのですか?

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

HTML:

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

jQ:

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

2 に答える 2

3
$(response)
          .filter(function() { 
             return $("body");
          })
          .appendTo("#output");

デモ

あなたもすることができます

$('<div/>')           // add response to fake div
    .append(response)  
    .find('body')     // find your target
    .appendTo('#output'); // append the target

デモ

于 2012-06-02T18:42:40.590 に答える
0

わかりました、これを理解しました。jQuery は、動的に生成された html で「html」および「body」タグに遭遇すると、黙って嘔吐します。これらのタグを置き換えるか削除するだけで、期待どおりに機能するようになりました。

http://jsfiddle.net/pqyeM/13/

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");
于 2012-06-02T22:13:59.923 に答える