[英]Get DOM from a string
給定一個包含整個頁面的HTML的String,我只需要主體的innerHTML。 如果不可以自己解析HTML,則可以從String中創建一個元素,然后直接獲取主體似乎更容易。
我發現了一些相關的東西,但是我無法上班(我再也找不到問題了)。
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
var ret = xmlhttp.responseText + "";
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(ret);
}
}
xmlhttp.open("GET", "http://url.php", false);
xmlhttp.send();
現在我有這個ajax請求,但是我只需要返回的主體。
我嘗試了document.createElement(ret).body
和new Element(ret).body
但是它們似乎沒有用。
var helper = document.createElement("html");
helper.innerHTML = ret;
body = helper.querySelector("body"); //Or getElementsByTagName("body")[0]
您可以使用simple_html_dom進行此操作,並使用PHP獲取整個頁面的HTML,然后僅獲取正文內容,如下所示
$html=file_get_html("url.php");
$body=$html->find("body");
$echo $body->plaintext
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.