首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

html代码如何转换成javascript的DOM对象

2012-11-12 
html代码怎么转换成javascript的DOM对象我是想通过抓取其他网站的页面,然后通过jquery进行DOM操作,抓取数

html代码怎么转换成javascript的DOM对象
我是想通过抓取其他网站的页面,然后通过jquery进行DOM操作,抓取数据.这样就不用写一堆正则了.

JScript code
<?php$html=<<<HTML<html><head><body><div class="test">    的方式对付的是是</div></body></head></html>HTML;?><html><head>    <script type="text/javascript">        var html = <?php echo json_encode($html) ?>;        console.log(html)//这个html怎么转化成DOM节点或者可以像document.getElementById();这样进行操作?    </script></head></html>


[解决办法]
HTML code
<script type="text/javascript">var html="<div id='theforever_csdn_id'>碧海情天<br>断水寒刀</div>"var theforever_csdn=document.createElement("div");theforever_csdn.innerHTML=html;theforever_csdn.style.visibility="hidden";document.appendChild(theforever_csdn);alert(document.getElementById("theforever_csdn_id").innerText);</script>
[解决办法]
JScript code
//假定你实际的html的值是正确的var html='<html><head><body><div class="test">test</div><div class="test2">2</div><div class="test3">3</div></body></head></html>';var dom=$('<div>').append(html);alert(dom.find('.test').html());/*//你也可以和下面一样,进行你需要的操作dom.find('.test').attr("id","test").end().find('.test2').attr("id","test").end().find('.test3').attr("id","test3");*/ 

热点排行
Bad Request.