【问题标题】:Stop HTML text being highlighted停止突出显示 HTML 文本
【发布时间】:2012-03-26 01:57:10
【问题描述】:

我的部分页面需要双击,这会产生不良影响,有时用户会无意中突出显示页面上的某些文本。

这真的很不整洁,但是除了使用图像而不是文本之外,有没有一种巧妙的方法来阻止这种情况的发生?

谢谢

【问题讨论】:

    标签: html text highlight


    【解决方案1】:

    正如@arunes 所建议的,您可以在大多数浏览器中使用 CSS 来实现这一点。但是我读到这不适用于 IE 6-8(至少)。因此,为此,您可能需要以下内容:

    document.getElementById("divDoubleClick").onselectstart = function() { return false; };
    

    【讨论】:

      【解决方案2】:

      这对我有用

      按照建议将css样式放入

      body, div
      {
          -webkit-touch-callout: none;
          -webkit-user-select: none;
          -khtml-user-select: none;
          -moz-user-select: -moz-none;
          -ms-user-select: none;
          -o-user-select: none;
          user-select: none;
      }
      

      还可以添加这些以允许在表单字段中进行选择

      input, textarea, .userselecton
      {
          -webkit-touch-callout: text;
          -webkit-user-select: text;
          -khtml-user-select: text;
          -moz-user-select: text;
          -ms-user-select: text;
          -o-user-select: text;
          user-select: text;
      }
      

      请注意需要的 -moz-none 或重新启用输入不起作用。

      对于IE我添加

      <script type="text/javascript">
          window.addEvent( "domready", function()
          {
              document.addEvent( "selectstart", function(e)
              {
                  if ( (e.target.tagName == "INPUT") ||
                   (e.target.tagName == "TEXTAREA") ||
                       (e.target.hasClass("userselecton")))
                  {
                      return true;
                  }
                  return false;
              } );
          } );
      </script>
      

      这不仅可以防止选择背景文本,还可以在输入字段和您放置 userseleton 类的元素上进行选择。

      【讨论】:

        【解决方案3】:

        这里是 css 禁用文本选择。 How to disable text selection highlighting using CSS?

        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        -o-user-select: none;
        user-select: none;
        

        【讨论】:

        • 对我来说已经足够好了!谢谢:)
        • 为什么要添加 -webkit-touch-callout: none;?
        猜你喜欢
        • 2013-01-23
        • 1970-01-01
        • 2011-11-20
        • 2013-08-18
        • 2010-10-18
        • 1970-01-01
        • 1970-01-01
        • 2018-08-29
        • 1970-01-01
        相关资源
        最近更新 更多