1

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

4 に答える 4

1

上記の例はすべて複雑すぎます..ブラウザのバージョンに基づいています。私は簡単な解決策を得ました...すべてのブラウザで動作します!

// 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) ....
于 2014-02-24T21:27:10.850 に答える
1

khtmlUserSelectの代わりにWebkit を使用する場合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 用にすでにある回避策が必要です。

于 2010-11-30T10:07:20.067 に答える
0

Firefox の MozUserSelect スタイルと同様に、-webkit-user-select: noneWebkit ベースのブラウザー (Safari や Chrome など) に使用できます。

-o-user-select: noneOperaで使えると思います。しかし、私はそれをテストしていません。

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

于 2010-11-30T10:09:28.773 に答える
0

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

于 2010-11-30T10:15:27.767 に答える