Home  >  Article  >  Web Front-end  >  selectNode and other methods of range object

selectNode and other methods of range object

高洛峰
高洛峰Original
2017-02-09 14:47:042703browse

selectNode and other methods of range objects

关于range对象的selectNodeContents、selectNode、deleteContents方法
示例代码如下:
<!DOCTYPE html>
<html>
<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="删除内容">
    <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>

For more articles related to selectNode and other methods of range objects, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn