【问题标题】:Clear Text Selection with JavaScript使用 JavaScript 清除文本选择
【发布时间】:2011-03-11 07:58:36
【问题描述】:

我找不到答案的简单问题:

如何使用 JavaScript(或 jQuery)取消选择网页上可能选择的任何文本?

例如用户单击并拖动以突出显示一些文本 - 我想要一个函数 deselectAll() 清除此选择。我该怎么写呢?

感谢您的帮助。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:
    if (window.getSelection) {
      if (window.getSelection().empty) {  // Chrome
        window.getSelection().empty();
      } else if (window.getSelection().removeAllRanges) {  // Firefox
        window.getSelection().removeAllRanges();
      }
    } else if (document.selection) {  // IE?
      document.selection.empty();
    }
    

    Credit to Mr. Y.

    【讨论】:

    • 这假定document.selection 的存在意味着它的empty() 方法的存在。您已经在所有其他情况下测试了该方法,因此您不妨在最后一种情况下也测试empty
    • 在我的jquery.tableCheckbox.js 插件中使用了这个,谢谢。
    • 我根据您的建议创建了一个完整的工作示例,但添加了一些额外内容jsfiddle.net/mkrivan/hohx4nes 最重要的一行是 window.getSelection().addRange(document.createRange());没有这个 IE 在某些情况下不会取消选择文本。而且我改变了方法检测的顺序。
    • 朋友,你拯救了我的一天!我正在使用暂存器,当刮擦整个页面时,我选择了这个好的脚本,然后在使用暂存器插件之前取消选择我的页面。基本上它有效!非常感谢!
    • 我发现window.getSelection().removeAllRanges(); 在 IE(edge) 和 Safari 中运行良好。
    【解决方案2】:

    最好直接测试你想要的功能:

    var sel = window.getSelection ? window.getSelection() : document.selection;
    if (sel) {
        if (sel.removeAllRanges) {
            sel.removeAllRanges();
        } else if (sel.empty) {
            sel.empty();
        }
    }
    

    【讨论】:

      【解决方案3】:

      2014 年退选事务状况

      我自己做了一些研究。这是我最近编写和使用的函数:

      (function deselect(){
        var selection = ('getSelection' in window)
          ? window.getSelection()
          : ('selection' in document)
            ? document.selection
            : null;
        if ('removeAllRanges' in selection) selection.removeAllRanges();
        else if ('empty' in selection) selection.empty();
      })();
      

      基本上,所有现代浏览器(包括 IE9+)目前都支持getSelection().removeAllRanges()。这显然是前进的正确方法。

      解决的兼容性问题:

      • 使用旧版本的 Chrome 和 Safari getSelection().empty()
      • IE8及以下使用document.selection.empty()

      更新

      包装此选择功能以供重复使用可能是个好主意。

      function ScSelection(){
        var sel=this;
        var selection = sel.selection = 
          'getSelection' in window
            ? window.getSelection()
            : 'selection' in document
              ? document.selection
              : null;
        sel.deselect = function(){
          if ('removeAllRanges' in selection) selection.removeAllRanges();
          else if ('empty' in selection) selection.empty();
          return sel; // chainable :)
        };
        sel.getParentElement = function(){
          if ('anchorNode' in selection) return selection.anchorNode.parentElement;
          else return selection.createRange().parentElement();
        };
      }
      
      // use it
      var sel = new ScSelection;
      var $parentSection = $(sel.getParentElement()).closest('section');
      sel.deselect();
      

      我已将其设为社区 wiki,以便您可以向其中添加功能,或随着标准的发展更新内容。

      【讨论】:

      • 这和我的回答一样。 4 年没有任何改变。
      • 太可怕了。你不仅真的从另一张海报中窃取了实现,而且在语法上完全破坏了它。
      【解决方案4】:

      这是公认的答案,但只有两行代码:

      var selection = window.getSelection ? window.getSelection() : document.selection ? document.selection : null;
      if(!!selection) selection.empty ? selection.empty() : selection.removeAllRanges();
      

      我不做的唯一检查是是否存在 removeAllRanges - 但 AFAIK 没有浏览器具有 window.getSelectiondocument.selection没有 有 @987654324 @ 或 .removeAllRanges 用于该属性。

      【讨论】:

        【解决方案5】:

        将此添加到您的脚本中以防止右键单击和文本选择。

        可以将异常添加到 var 'om'。

        var d=document,om=['input','textarea','select'];;
        function ie(){if(d.all){(mg);return false;}}function mz(e){if(d.layers||(d.getElementById&&!d.all)){if(e.which==2||e.which==3){(mg);return false;}}}if(d.layers){d.captureEvents(Event.mousedown);d.onmousedown=mz;}else{d.onmouseup=mz;d.oncontextmenu=ie;}d.oncontextmenu=new Function('return false');om=om.join('|');function ds(e){if(om.indexOf(e.target.tagName.toLowerCase())==-1);return false;}function rn(){return true;}if(typeof d.onselectstart!='undefined')d.onselectstart=new Function('return false');else{d.onmousedown=ds;d.onmouseup=rn;}
        

        【讨论】:

        • 这不是被问到的问题。很明显,选择是必需的,但 OP 正在寻找一种在不再需要时以编程方式取消选择区域的方法。
        【解决方案6】:

        2021 年答案

        • 大多数浏览器都支持

          removeAllRanges(),macOS 或 iOS 上的 Safari 除外。

        • empty()removeAllRanges() 的别名,由all browsers 支持,包括非常旧的,IE 除外。这个别名是defined in the specification,所以应该可以安全地依赖。

        结论

        只需使用getSelection().empty()。其他答案中不需要辅助函数、嵌套的三元 if、构造函数和诸如此类的 Ninja banzai。也许十年前需要,但现在不需要了。

        如果你真的需要 IE 支持,你可以测试document.selection:

        (window.getSelection ? window.getSelection() : document.selection).empty()
        

        (未在 IE 上测试)

        【讨论】:

        • removeAllRanges() 在 macOS 和 iOS 的 Safari 中都受支持,除非它已被删除,我非常怀疑它被广泛使用。我同意现在删除对真正旧的 IE (window.getSelection().removeAllRanges(),它也将在 IE 中返回到版本 9。另请注意,在某些(罕见)情况下,window.getSelection() 可以返回null,因此可能值得检查一下,因此有些忍者万岁之类的其他答案。
        猜你喜欢
        • 1970-01-01
        • 2014-06-10
        • 1970-01-01
        • 2019-02-22
        • 2012-03-17
        • 1970-01-01
        • 2021-10-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多