因为div设置为contenteitable可编辑的文本内容用 select()选择全部内容不生效,所以只能用下列方法:

先 creatTextRange或者 createRange

 <div contenteditable='true' id="selectable" onclick="selectText('selectable')">http://example.com/page.htm</div>
    
    <script type="text/javascript">
        function selectText(containerid) {
            if (document.selection) {
                var range = document.body.createTextRange();
                range.moveToElementText(document.getElementById(containerid));
                range.select();
            } else if (window.getSelection) {
                var range = document.createRange();
                range.selectNodeContents(document.getElementById(containerid));
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(range);
            }
        }
    </script>