代码之家  ›  专栏  ›  技术社区  ›  Reigel Gallarde

在jQuery[duplicate]中双击时禁用文本突出显示

  •  50
  • Reigel Gallarde  · 技术社区  · 16 年前

    我有这个jQuery切换。它很好用。

       <ul>
        <li>Go to the store</li>
        <li>Pick up dinner</li>
        <li>Debug crash</li>
        <li>Take a jog</li>
      </ul>
    

            $("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":""});
              }
            );
    

    有没有办法阻止文本突出显示?

    9 回复  |  直到 15 年前
        1
  •  54
  •   David Thomas    15 年前

    disable text selection with jQuery .


    编辑 http://chris-barr.com/index.php/entry/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'
    });
    

    Chris Barr, of chris-barr.com ,于2008年12月21日星期五访问 2011年1月31日。

        2
  •  37
  •   Sjoerd    12 年前

    user-select :

    .unselectable {
      -moz-user-select: none;
      -webkit-user-select: none;
      -ms-user-select: none;
    }
    
        3
  •  36
  •   VisioN    13 年前

    如果使用jQuery UI,则可以禁用如下简单的文本选择:

    $("body").disableSelection();
    
        4
  •  10
  •   Alex Bagnolini    16 年前
    //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);
    

    Source

        5
  •  7
  •   nonzaprej    6 年前

    li.noselection::selection {
        background-color: transparent;
    }
    

    从7点到10点,我在Chrome、Firefox、EDGE和IE上进行了测试。

        6
  •  4
  •   Lahmizzar    12 年前

    使用1.9.x以上的jQuery版本

    HTML

    html标记如上图所示:

    <ul>
        <li>Go to the store</li>
        <li>Pick up dinner</li>
        <li>Debug crash</li>
        <li>Take a jog</li>
    </ul>
    

    预防默认值() 返回错误 这不会杀死你可能拥有的任何其他处理程序

    $('li').on('selectstart', function (event) {
        event.preventDefault();
    });
    

    例子: jsFiddle

        7
  •  0
  •   tim    14 年前
    window.getSelection().empty()
    

    在Chrome上可以很好地工作,尽管有一个快速闪烁的选择,这是丑陋的。

        8
  •  0
  •   pelister    12 年前
    $( 'selector' ).on( 'selectstart', 'selector', function( ) { 
      return false; 
    }).css( 'MozUserSelect','none' ).mousedown( function( ) {
      return false;
    }); 
    

    将选择器替换为您自己的-此代码在所有浏览器上都可以正常工作。对动态插入到DOM中的元素使用.on()

        9
  •  0
  •   Mike Furlender Kumar Pankaj Dubey    12 年前
    document.onselectstart = function() { return false; }
    document.onmousedown = function() { return false; }