如何查询从 AJAX 返回的 HTML?
我试过了
$.post("gethtml.php", { url: $url.val() }, function(data) {
var $html = $(data),
$links = $("link, script, style", $html),
$body = $("body", $html);
$links.each(function() { $(this).addClass("tmp"); });
console.log($html);
console.log($body.html());
});
$html
看起来像 [meta, title, script, style#gstyle, script, textarea#csi, div#mngb, iframe, center, script, script]
和 $body.html()
为空
更新
http://jsfiddle.net/uvnrJ/1/ 上的简单设置
$(function() {
var html = "<!doctype html><html><head><title>title here ... </title></head><body>This is the body</body></html>",
$html = $(html);
console.log($html); // jQuery(title, <TextNode textContent="This is the body">)
console.log($html.find("body")); // jQuery()
console.log($html.find("title")); // jQuery()
console.log($html.filter("title")); // jQuery(title)
});
显示 jQuery 似乎在解析 HTML 字符串时有问题?
最佳答案
试试这个...
..........
var elements = $('<div></div>');
elements.html(data);
alert(elements.find('body').html());
...........
关于jquery - 查询从 AJAX 返回的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5401185/