效果:

代码:

function selectText(element) {
        var text = document.getElementById(element);//document.getElementsByClassName(element);
        if (document.body.createTextRange) {
            var range = document.body.createTextRange();
            range.moveToElementText(text);
            range.select();
        } else if (window.getSelection) {
            var selection = window.getSelection();
            var range = document.createRange();
            range.selectNodeContents(text);
            selection.removeAllRanges();
            selection.addRange(range); 
        } else {
            alert("无内容");
        }
    }
 

调用:

selectText('mydiv');