从responseText获取特定的div内容并将源代码保存到数据库中

时间:2023-01-26 16:35:36

I'm trying to get a specific div content including all the elements of another page from responseText

我正在尝试获取特定的div内容,包括来自responseText的另一个页面的所有元素

the body of the html file looks like this:

html文件的主体如下所示:

<div id="div_1">
    <div><h1> Title 1 </h1></div>
    <div><img src="blah1.jpg" /></div>
    <div><p> Lorem Ipsum </p></div>
</div>
<div id="div_2">
    <div><h1> Title 2 </h1></div>
    <div><img src="blah2.jpg" /></div>
    <div><p> Lorem Ipsum </p></div>
</div>

here is my function to get the source code of the entire html page:

这是我获取整个html页面源代码的函数:

function getSourceCode() {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            var source_code = xmlhttp.responseText;
            alert(source_code);
        }
    }
    xmlhttp.open("GET","test.html",true);
    xmlhttp.send(); 
}

my question now is how to get only the source code for the "div_1" element. I want to save this source code into a database. I tried to use getElementbyId("div_1") but it didn't work.

我现在的问题是如何只获取“div_1”元素的源代码。我想将此源代码保存到数据库中。我试图使用getElementbyId(“div_1”),但它没有用。

1 个解决方案

#1


2  

Try to .filter() out the required element and get its innerHtml by using .html(),

尝试.filter()输出所需的元素,并使用.html()获取其innerHtml,

$(source_code).filter('#div_1').html()

#1


2  

Try to .filter() out the required element and get its innerHtml by using .html(),

尝试.filter()输出所需的元素,并使用.html()获取其innerHtml,

$(source_code).filter('#div_1').html()