在jQuery中双击禁用文本突出显示

Rei*_*gel 51 javascript jquery text

我有这个jQuery切换.它工作正常.

   <ul>
    <li>Go to the store</li>
    <li>Pick up dinner</li>
    <li>Debug crash</li>
    <li>Take a jog</li>
  </ul>
Run Code Online (Sandbox Code Playgroud)

 

        $("li").toggle(
          function () {
            $(this).css({"list-style-type":"disc", "color":"blue"});
          },
          function () {
            $(this).css({"list-style-type":"disc", "color":"red"});
          },
          function () {
            $(this).css({"list-style-type":"", "color":""});
          }
        );
Run Code Online (Sandbox Code Playgroud)

问题是当我快速点击时,它会突出显示其中的文字.有没有办法阻止文本突出显示?

Dav*_*mas 53

我在iPhone上写字,而远离桌面,但是快速谷歌出现了这个页面:用jQuery禁用文本选择.


编辑以回应"死链接"评论(来自@Herb Caudill).虽然原来的链接确实已经死了,但似乎是由于网站重组(而不是删除),文章的新位置可以在这里找到:http://chris-barr.com/index.php/进入/ disable_text_selection_with_jquery /

该条款中提供的代码转载如下:

$(function(){
    $.extend($.fn.disableTextSelect = function() {
        return this.each(function(){
            if($.browser.mozilla){//Firefox
                $(this).css('MozUserSelect','none');
            }else if($.browser.msie){//IE
                $(this).bind('selectstart',function(){return false;});
            }else{//Opera, etc.
                $(this).mousedown(function(){return false;});
            }
        });
    });
    $('.noSelect').disableTextSelect();//No text selection on elements with a class of 'noSelect'
});
Run Code Online (Sandbox Code Playgroud)

Chris-barr.comChris Barr撰写的jQuery片段,于2011年1月21 星期五访问.

  • $ .browser已被弃用,并且绑定到浏览器不支持的事件无论如何都不会有任何恶意.所以我建议简单地将"selectstart","click"和"mousedown"绑定到返回false的函数.这也适用于Mozilla,不需要CSS属性MozUserSelect. (7认同)
  • 这可以防止任何文本选择,例如单击/拖动以突出显示 (2认同)
  • +1给@madbreaks评论 - 这不回答问题; 它会阻止*all*文本选择,而不仅仅是双击选择. (2认同)

Vis*_*ioN 36

如果您使用jQuery UI,则可以禁用文本选择,如下所示:

$("body").disableSelection();
Run Code Online (Sandbox Code Playgroud)

  • 一定要喜欢jQuery UI :)谢谢! (6认同)
  • 简单&做到了.谢谢!浏览器检测方法已在jQuery 1.9中删除. (3认同)

Sjo*_*erd 36

我使用非标准CSS关键字user-select解决了这个问题:

.unselectable {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*ini 10

//function to be reused later
function clearSelection() {
  var sel ;
  if(document.selection && document.selection.empty){
    document.selection.empty() ;
  } else if(window.getSelection) {
    sel=window.getSelection();
    if(sel && sel.removeAllRanges)
      sel.removeAllRanges() ;
  }
}

$('p').click(clearSelection);
Run Code Online (Sandbox Code Playgroud)

资源


non*_*rej 7

我有同样的问题,这对我有用:

li.noselection::selection {
    background-color: transparent;
}
Run Code Online (Sandbox Code Playgroud)

我在Chrome,IE从7到10和Firefox上测试过它.

PS这只会禁用"视觉效果",文本仍会被选中.我希望这就是为什么这个被低估了,因为如果你的问题只是美学,那么这个解决方案可以100%运行.


Lah*_*zar 5

使用 1.9.x 以上的 jQuery 版本

超文本标记语言

html标记如上所示:

<ul>
    <li>Go to the store</li>
    <li>Pick up dinner</li>
    <li>Debug crash</li>
    <li>Take a jog</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

JS

使用PreventDefault()而不是return false,这不会杀死您可能拥有的任何其他处理程序

$('li').on('selectstart', function (event) {
    event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)

示例:jsFiddle