html代码怎么转换成javascript的DOM对象
我是想通过抓取其他网站的页面,然后通过jquery进行DOM操作,抓取数据.这样就不用写一堆正则了.
<?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>
<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>
[解决办法]
//假定你实际的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");*/