查询HTML的AJAX返回HTML、AJAX

2023-09-10 21:33:49 作者:狗咬狗一嘴毛

我如何可以查询HTML多数民众赞成从阿贾克斯回来?

How can I query HTML thats returned from AJAX?

我试过

$.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 看起来像 [元,标题,文字,样式#gstyle,脚本,文本区域#CSI,DIV#mngb,iframe中,中心,脚本,脚本] $ 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字符串的问题?

Shows that jQuery seem to have problems parsing the HTML string?

推荐答案

试试这个...

..........
var elements = $('<div></div>');
elements.html(data);
alert(elements.find('body').html());
...........