【问题标题】:JavaScript non selectable text not working in Chrome/Internet ExplorerJavaScript 不可选择的文本在 Chrome/Internet Explorer 中不起作用
【发布时间】:2010-11-30 10:00:03
【问题描述】:

在 Firefox 中看起来不错,Chrome 和 Internet Explorer 的文本仍然是可选的,有什么办法可以解决这个问题吗?代码取自另一个问题(我现在找不到)所以它可能已经过时了?

// Prevent selection
function disableSelection(target) {
    if (typeof target.onselectstart != "undefined") // Internet Explorer route
        target.onselectstart = function() { return false }
    else if (typeof target.style.MozUserSelect != "undefined") // Firefox route
        target.style.MozUserSelect = "none"
    else // All other routes (for example, Opera)
        target.onmousedown = function() { return false }
}

在代码中用作:

disableSelection(document.getElementById("gBar"));

【问题讨论】:

  • 为什么要这样做?即使你让它工作,再次选择文本就像禁用 javascipt 或查看源代码一样简单。真正阻止选择的唯一方法是提供图像,这会导致各种可访问性问题并隐藏搜索引擎的文本。
  • 我知道有人会问这个大声笑,我完全意识到这些缺点,但这不是为了数据保护,它只是为了提高我正在制作的网络应用程序的性能,所以当用户拖放元素时,其中包含的文本不会开始被选中,从而使应用程序行为异常。
  • 我最近遇到了这样的问题,并通过 jQuery 升级解决了它:)
  • 我没用Jquery,都是自己写的(部分是为了更好地学习JS)

标签: javascript text jquery-ui-selectable


【解决方案1】:

对于 webkit,使用 khtmlUserSelect 而不是 MozUserSelect

在 Opera 和 MSIE 中,您可以将 unselectable-property 设置为“On”

由于gecko/webkit相关的样式都是CSS,所以可以使用一个类来应用:

<script type="text/javascript">
<!--
function disableSelection(target)
{
  target.className='unselectable';
  target.setAttribute('unselectable','on');
}
//-->
</script>
<style type="text/css">
<!--
.unselectable{
-moz-user-select:none;
-khtml-user-select: none;
}
-->
</style>

注意:unselectable 不会传递子元素,因此,如果目标中除了 textNodes 之外还有其他内容,则需要使用 MSIE/opera 已有的解决方法。

【讨论】:

  • 没有。对于 webkit,使用 webkit 前缀
【解决方案2】:

以上所有例子都太复杂了..基于浏览器版本。 我得到了简单的解决方案……适用于所有浏览器!

// you can select here which html element you allow to be selected
var ExcludeElems = ["INPUT","SELECT","OPTION"]



function disableSelection (target) {
   // For all browswers code will work .....
   target.onmousedown = function (e) 
  {
     var i;
     var e = e ? e : window.event;

     if (e) 
      for (i=0; i<ExcludeElems.length;i++)
        if (e.target.nodeName == ExcludeElems[i] ) 
           return true;
     return false;
  }

如果你需要,你可以使这个函数更复杂。 将此代码用于任何容器元素...

disableSelection (document) 
//disableSelection (document.body) 
//disableSelection (divName) ....

【讨论】:

    【解决方案3】:

    对于 Wekbit(例如 Chrome 和 Safari),您可以添加:

    else if (typeof target.style.webkitUserSelect != "undefined") // Webkit route
        target.style.webkitUserSelect = "none";
    

    对于 IE,使用“不可选择”:

    else if (typeof target.unselectable != "undefined") // IE route
        target.unselectable = true;
    

    参考:http://help.dottoro.com/ljrlukea.php

    【讨论】:

    【解决方案4】:

    与 Firefox 中的 MozUserSelect 样式一样,您可以将 -webkit-user-select: none 用于基于 Webkit 的浏览器(如 Safari 和 Chrome)。

    我认为你可以在 Opera 中使用-o-user-select: none。但是我没有测试过。

    // Prevent selection
    function disableSelection(target) {
        if (typeof target.onselectstart != "undefined") //IE route
            target.onselectstart = function() { return false }
        else if (typeof target.style.userSelect != "undefined") //Some day in the future?
            target.style.userSelect = "none"
        else if (typeof target.style.webkitUserSelect != "undefined") //Webkit route
            target.style.webkitUserSelect = "none"
        else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
            target.style.MozUserSelect = "none"
        else //All other route (ie: Opera)
            target.onmousedown = function() { return false }
    }
    

    对于 IE,也许这可以帮助你:http://msdn.microsoft.com/en-us/library/ms534706(VS.85).aspx

    【讨论】:

    • 感谢您的代码,但这仍然与我在所有 3 个浏览器中的原始代码完全相同(适用于 FF 但不适用于 chrome/IE)
    • 对不起,痛苦,仍然没有快乐。 div 包含在其他可选 div 中的事实是否可能是问题的一部分?
    • 在 Safari 和 Chrome 中为我工作。但你可能应该按照莫勒博士所写的那样去做。所以把样式放在一个 css 类中并设置在目标上。
    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 2013-11-18
    • 2012-01-04
    • 2011-01-25
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多