douhan9191 2013-01-25 00:27
浏览 41
已采纳

为什么cant jquery找不到并返回所有html标签,即body / title

I have spent ages at this and dont understand why it is not working. I am using ajax to send a post to php curl which brings a url in it then goes back to ajax. When I send an alert all the html of that page is displayed in the alert. Now I want to use jquery to find each element i.e. body, h1, title and output the contents but when I do this it doesnt work for the body or title. Why is that?

Here is my jquery

        $.ajax({
      type: "POST",
      url: "senddom.php",
      data: {"dataString" : dataString },
      success: function(response) {   
  $('body').append("<p> contents of title:" + $(this).find('title').html() + "</p>");       
  $('body').append("<p> contents of all: " + $(this).find('body').html() + "</p>");

 $(response).find('h1').each(function() {
  $('body').append("<p> contents of H1: " + $(this).text() + "</p>");
});

this is my php

<?php 

    $site= $_POST['dataString'];             // get data
function curl_get($site){
    $useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$site);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

    $data=curl_exec($ch);
    curl_close($ch);
    return $data;


}

function getdom($site){
    $html = curl_get($site);
    $xml = new DOMDocument();
    @$xml->loadHTML($html);
    echo($html);


}

echo getdom($site);

?>
  • 写回答

1条回答 默认 最新

  • dongshuan8722 2013-01-25 00:30
    关注

    inside your success block: $(this) -> $(response)

    Follow Up: Full text HTML vs Fragments

    While this may seem to take care of the immediate problem, it is dependent on whether or not your response is the full body HTML.

    In this case, it seems as though response is the full text HTML.

    The jQuery parseHTML method does not deal with full text HTML, and unfortunately response won't be parsed properly. Fore more info see: https://github.com/jquery/jquery/blob/master/src/core.js

    In the case where you are looking to parse full text HTML, one approach would be to use the DOMParser API. It is worth noting that the support for parsing "text/html" is currently lacking; only available in Firefox. An alternative is document.implementation which has support in all but IE<9.

    That said, an example in the case where you want to use DOMParser --

    parser = new DOMParser();
    doc = parser.parseFromString(response, "text/html");
    $response = $(doc)
    $response.find('title').text()
    

    An example in the where you want to use documentation.implementation --

    var doc = document.implementation.createHTMLDocument("");
    var doc_el = doc.documentElement;
    doc_el.innerHTML = response;
    $response = $(doc_el); // 
    $response.find('title').text()
    

    For more information, I'll defer to a nice jQuery plugin (that deals with the x-browser support) that I found on github: jquery.parsehtml.js.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog