简体   繁体   English

使所有文本除外 <input> 在Internet Explorer中无法选择?

[英]Make all text EXCEPT <input> unselectable in Internet Explorer?

I have a website where I want to disable users from selecting content EXCEPT for input areas. 我有一个网站,我想禁止用户选择内容除了输入区域。 I currently have some CSS to disable user-select: 我目前有一些CSS禁用用户选择:

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;

However, this does NOT cover Internet Explorer; 但是,这不包括Internet Explorer; thus, I need to implement some JavaScript: 因此,我需要实现一些JavaScript:

<body onselectstart="return false;">

Through CSS and JavaScript, I can make all content unselectable across all popular browsers. 通过CSS和JavaScript,我可以在所有流行的浏览器中使所有内容无法选择。 BUT, this code also makes areas unselectable, which is a major case of poor usability. 但是,这段代码也使区域无法选择,这是可用性差的主要原因。 I use CSS to make input areas selectable: 我使用CSS来输入输入区域:

-webkit-user-select: text;
-khtml-user-select: text;
-moz-user-select: text;
-o-user-select: text; 
user-select: text;

.. and as you might have expected, this does not cover Internet Explorer, since I used JavaScript to disable all content from being selectable. ..并且正如您可能预料的那样,这不包括Internet Explorer,因为我使用JavaScript禁用所有内容可供选择。

What can I do to make all content unselectable except for input areas? 除了输入区域,我该怎么做才能使所有内容无法选择?

Try this one: oncontextmenu="return false;" 试试这个:oncontextmenu =“return false;”

Put that in your body tag, then use something like: 把它放在你的身体标签中,然后使用类似的东西:

e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();

in a javascript function for the input items you want selectable. 在javascript函数中,您可以选择输入项目。 That should stop the propagation of the event that would trigger the body tag. 这应该会阻止触发body标签的事件的传播。

由于事件冒泡到身体而不是源自身体,我认为您可以检查实际目标节点的节点名称,并避免为某些节点上发生的事件返回false:

<body onselectstart="if ((event.target || event.srcElement).nodeName !== 'INPUT') return false;">

You can add the proprietary IE attribute unselectable="on" to any element that you want to make unselectable in IE: 您可以将专有IE属性unselectable="on"到要在IE中无法选择的任何元素:

<p unselectable="on">I don't want IE users to easily select this text 
  for some reason.</p>

See Making things unselectable in IE for a more detailed explanation. 有关更详细的说明,请参阅在IE中使某些内容无法选择

If doing this from javascript, be sure to use el.setAttribute("unselectable","on") . 如果从javascript执行此操作,请务必使用el.setAttribute("unselectable","on") Just using el.unselectable="on" will not work. 只使用el.unselectable="on"将无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM