且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

选择与& quot; Ctrl + A& quot;类似的文本单击文本时?

更新时间:2023-02-15 19:35:12

以下是一个函数,它将选择传递给它的元素的内容:

Here's a function that will select the contents of the element you pass to it:

function selectElementContents(el) {
    var range;
    if (window.getSelection && document.createRange) {
        range = document.createRange();
        var sel = window.getSelection();
        range.selectNodeContents(el);
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (document.body && document.body.createTextRange) {
        range = document.body.createTextRange();
        range.moveToElementText(el);
        range.select();
    }
}

window.onload = function() {
    var el = document.getElementById("your_para_id");
    selectElementContents(el);
};