Selecting text in an element (akin to highlighting with your mouse)

后端 未结 16 1676
孤街浪徒
孤街浪徒 2020-11-21 05:58

I would like to have users click a link, then it selects the HTML text in another element (not an input).

By \"select\" I mean the same way you would select

16条回答
  •  情深已故
    2020-11-21 06:18

    Tim's method works perfectly for my case - selecting the text in a div for both IE and FF after I replaced the following statement:

    range.moveToElementText(text);
    

    with the following:

    range.moveToElementText(el);
    

    The text in the div is selected by clicking it with the following jQuery function:

    $(function () {
        $("#divFoo").click(function () {
            selectElementText(document.getElementById("divFoo"));
        })
    });
    

提交回复
热议问题