单击鼠标选择所有 DIV 文本

IT技术 javascript css
2021-01-17 05:33:15

当用户单击 DIV 时如何突出显示/选择 DIV 标签的内容......这个想法是突出显示/选择所有文本,因此用户不需要用鼠标手动突出显示文本,并且可能错过了一点文字?

例如,假设我们有一个如下所示的 DIV:

<div id="selectable">http://example.com/page.htm</div>

...并且当用户单击该 URL 中的任何一个时,整个 URL 文本都会突出显示,因此他们可以轻松地在浏览器中拖动所选文本,或通过右键单击复制完整的 URL。

谢谢!

6个回答

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

现在您必须将 ID 作为参数传递,在这种情况下它是“可选的”,但它更具全局性,允许您在任何地方多次使用它,而无需使用 chiborg 提到的 jQuery。

在 Chrome 36+ 版本中,这将返回错误“不支持不连续选择”。解决方法是在window.getSelection().removeAllRanges();之前添加window.getSelection().addRange(range);
2021-03-16 05:33:15
这适用于 Chrome、FF、Safari (Mac) 以及 Chrome 和 IE(Windows 9+,8 未测试)。但它似乎不适用于 iPad Mini (iOS6) 或 iPhone 4 上的 Safari,不确定其他 iOS 或 Android。
2021-03-20 05:33:15
顺便说一句,你可以很容易地把它变成一个 jQuery click 事件处理程序,替换document.getElementById('selectable')this. 然后,您可以不显眼地将功能添加到多个元素,例如容器中的多个 div: jQuery('#selectcontainer div').click(selectText);
2021-03-26 05:33:15
此解决方案似乎不适用于 ie11。知道为什么吗?
2021-03-28 05:33:15
根据本文,if (window.getSelection) {Opera的查询应首先出现 ( quirksmode.org/dom/range_intro.html )
2021-03-30 05:33:15

2017 年更新:

要选择节点的内容调用:

window.getSelection().selectAllChildren(
    document.getElementById(id)
);

这适用于所有现代浏览器,包括 IE9+(在标准模式下)。

可运行示例:

function select(id) {
  window.getSelection()
    .selectAllChildren(
      document.getElementById("target-div") 
    );
}
#outer-div  { padding: 1rem; background-color: #fff0f0; }
#target-div { padding: 1rem; background-color: #f0fff0; }
button      { margin: 1rem; }
<div id="outer-div">
  <div id="target-div">
    Some content for the 
    <br>Target DIV
  </div>
</div>

<button onclick="select(id);">Click to SELECT Contents of #target-div</button>


下面的原始答案已过时,因为window.getSelection().addRange(range); 已被弃用

原答案:

上面的所有示例都使用:

    var range = document.createRange();
    range.selectNode( ... );

但问题在于它选择节点本身,包括 DIV 标签等。

要根据您需要调用的 OP 问题选择节点的文本:

    range.selectNodeContents( ... )

所以完整的片段将是:

    function selectText( containerid ) {

        var node = document.getElementById( containerid );

        if ( document.selection ) {
            var range = document.body.createTextRange();
            range.moveToElementText( node  );
            range.select();
        } else if ( window.getSelection ) {
            var range = document.createRange();
            range.selectNodeContents( node );
            window.getSelection().removeAllRanges();
            window.getSelection().addRange( range );
        }
    }
您也可以使用this而不是根据 ID 获取元素,只要它在元素的click侦听器中即可。
2021-03-30 05:33:15

有纯 CSS4 解决方案:

.selectable{
    -webkit-touch-callout: all; /* iOS Safari */
    -webkit-user-select: all; /* Safari */
    -khtml-user-select: all; /* Konqueror HTML */
    -moz-user-select: all; /* Firefox */
    -ms-user-select: all; /* Internet Explorer/Edge */
    user-select: all; /* Chrome and Opera */

}

user-select是 CSS Module Level 4 规范,目前是草案和非标准 CSS 属性,但浏览器很好地支持它 - 请参阅#search=user-select

.selectable{
    -webkit-touch-callout: all; /* iOS Safari */
    -webkit-user-select: all; /* Safari */
    -khtml-user-select: all; /* Konqueror HTML */
    -moz-user-select: all; /* Firefox */
    -ms-user-select: all; /* Internet Explorer/Edge */
    user-select: all; /* Chrome and Opera */

}
<div class="selectable">
click and all this will be selected
</div>

在 MDN上阅读有关用户选择的更多信息,在 w3scools 中使用它

+1 真棒优雅的解决方案!2017 年 9 月测试,它在 FireFox 和 Chrome 上完美运行,但不适用于 MICROSOFT EDGE!?任何想法为什么不以及如何解决这个问题?谢谢!
2021-03-23 05:33:15
Mino,谢谢这有效!一键选择整个元素。但是,它无法滑动选择文本的子集。. . . 你可能的意思是-webkit-touch-callout: none;我收到了警告all无论如何,这个设置在这里做什么?. . . @Sam 在 2020 年我看到user-select了 MS Edge 上的工作。理智的头脑可能占了上风。
2021-04-02 05:33:15

Neuroxik 的回答真的很有帮助。我只有 Chrome 有问题,因为当我点击外部 div 时,它不起作用。我可以解决它在添加新范围之前删除旧范围:

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.selectNode(document.getElementById(containerid));
        window.getSelection().removeAllRanges();
        window.getSelection().addRange(range);
    }
}
<div id="selectable" onclick="selectText('selectable')">http://example.com/page.htm</div>

对于内容可编辑的内容(不是常规输入,您需要使用 selectNodeContents(而不仅仅是 selectNode)。

注意:所有对“document.selection”和“createTextRange()”的引用都是针对 IE 8 及更低版本的……如果你试图做这样棘手的事情,你可能不需要支持那个怪物。

function selectElemText(elem) {

    //Create a range (a range is a like the selection but invisible)
    var range = document.createRange();

    // Select the entire contents of the element
    range.selectNodeContents(elem);

    // Don't select, just positioning caret:
    // In front 
    // range.collapse();
    // Behind:
    // range.collapse(false);

    // Get the selection object
    var selection = window.getSelection();

    // Remove any current selections
    selection.removeAllRanges();

    // Make the range you have just created the visible selection
    selection.addRange(range);

}