日期:2014-05-16 浏览次数:20357 次
<?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"); */