JavaScript 不可选择文本在 Chrome/Internet Explorer 中不起作用

发布于 2024-10-04 17:52:15 字数 651 浏览 4 评论 0原文

在 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"));

In Firefox seems fine, Chrome and Internet Explorer the text is still selectable, is there any way around this? The code was taken from another question, (which I can't find right now) so it may be out of date?

// 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 }
}

Used in code as:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

杀手六號 2024-10-11 17:52:15

对于 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 已有的解决方法。

For webkit use khtmlUserSelect instead of MozUserSelect .

In opera and MSIE you may set the unselectable-property to "On"

As the both styles related to gecko/webkit are CSS, you can use a class to apply it:

<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>

Note: unselectable will not pass on child-elements, so if you have there anything else than textNodes inside target, you need the workaround you already have there for MSIE/opera.

独夜无伴 2024-10-11 17:52:15

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

// 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) ....

all of the above examples is too complicated.. based on the browser version.
I got simle solution ... works for all browsers!

// 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;
  }

if you need you can make this function more complicated.
Use this code for any container element ...

disableSelection (document) 
//disableSelection (document.body) 
//disableSelection (divName) ....
把梦留给海 2024-10-11 17:52:15

对于 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

For Wekbit (e.g. Chrome and Safari) you can add:

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

For IE, use 'unselectable':

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

Reference: http://help.dottoro.com/ljrlukea.php

自找没趣 2024-10-11 17:52:15

与 Firefox 中的 MozUserSelect 样式类似,您可以对基于 Webkit 的浏览器(例如 Safari 和 Chrome)使用 -webkit-user-select: none

我认为你可以在 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

Like the MozUserSelect styling in Firefox you can use -webkit-user-select: none for Webkit based browser (like Safari and Chrome).

I think that you can use -o-user-select: none in Opera. But I have not tested it.

// 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 }
}

For IE, maybe this can help you: http://msdn.microsoft.com/en-us/library/ms534706(VS.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文