關(guān)于range對象的selectNodeContents、selectNode、deleteContents方法
示例代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="div" style="background-color:rosybrown;width:500px;height:200px;">這里有文字</div> <input type="button" onclick="deleteContent(true)" value="刪除內(nèi)容"> <input type="button" onclick="deleteContent(false)" value="刪除元素"> <script> function deleteContent(test) { var div=document.getElementById("div"); var range=document.createRange(); if (test){ range.selectNodeContents(div); range.deleteContents(); }else{ range.selectNode(div); range.deleteContents(); } } </script> </body> </html>
【 微信掃一掃 】