2011-03-23 5 views
3

Comment puis-je interroger le code HTML renvoyé par AJAX?Requête HTML retournée par AJAX

J'ai essayé

$.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 ressemble [meta, title, script, style#gstyle, script, textarea#csi, div#mngb, iframe, center, script, script] et $body.html() null

MISE À JOUR

Une configuration simple à 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) 
}); 

Indique que jQuery semble avoir des problèmes pour analyser la chaîne HTML?

+0

qu'est-ce que vous essayez de faire? –

+1

Avez-vous essayé '$ body = $ (data) .find ('body');'? –

+0

@expérienceX J'essaie d'obtenir la source HTML d'une autre page Web. –

Répondre

0

Je pense que vous devriez rechercher vos éléments directement dans la variable data ou utiliser la méthode find() sur $(data). Quelque chose comme:

$.post("gethtml.php", { url: $url.val() }, function(data) { 
    var $html = $(data).find('html'), // First way 
     $links = $("link, script, style", data), // Second way 
     $body = $("body", data); // Could be better written as $(data).find('body') 

    $links.each(function() { $(this).addClass("tmp"); }); 
    console.log($html); 
    console.log($body.html()); 
}); 
+0

Cela ne semble pas fonctionner, vous pouvez essayer une installation très simple http://jsfiddle.net/uvnrJ/1/ comme vous pouvez le voir, jQuery semble détecter uniquement le tag de titre + un nœud de texte en quelque sorte. Je pense que c'est la cause du problème. Aucune suggestion? –

+0

@jiewmeng Oui, il semble que ce n'est pas possible d'utiliser jQuery. Jetez un oeil à cette réponse [jquery ajax parse texte de réponse] (http://stackoverflow.com/questions/1050333/jquery-ajax-parse-response-text) et en particulier celui-ci [Quelle est la meilleure pratique pour l'analyse à distance content with jQuery?] (http://stackoverflow.com/questions/1034881/what-is-the-best-practice-for-parsing-remote-content-with-jquery) –

0

je suis tombé sur la même question et a été contraint de recourir à l'analyse de la chaîne HTML directement:

var headStartIndex = htmlString.toLowerCase().indexOf("<head>") + "<head>".length; 
    var headEndIndex = htmlString.toLowerCase().indexOf("</head>"); 
    var newHead = htmlString.substring(headStartIndex, headEndIndex); 

    var bodyStartIndex = htmlString.toLowerCase().indexOf("<body>") + "<body>".length; 
    var bodyEndIndex = htmlString.toLowerCase().indexOf("</body>"); 
    var newBody = htmlString.substring(bodyStartIndex, bodyEndIndex); 

    console.log(newHead); 
    console.log(newBody); 
3

Essayez cette ...

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

vous peut essayer ceci, cela fonctionne

$.ajax({ 
    'url': url, 
    'dataType': 'html', 
    'success': function(e){ 
     var $div = $("<div id='_some_id'>" + e + "</div>"); 
     $div = $div.filter("_some_id"); 
     $div.find("span"); // you can search for anything here. it would work now 
    } 
}); 
Questions connexes