使所有文字除& lt; input& gt;在Internet Explorer中无法选择? [英] Make all text EXCEPT <input> unselectable in Internet Explorer?
问题描述
我有一个网站,我希望禁止用户选择输入区域以外的内容.我目前有一些CSS可以禁用用户选择:
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:
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
但是,这并不涵盖Internet Explorer;因此,我需要实现一些JavaScript:
However, this does NOT cover Internet Explorer; thus, I need to implement some JavaScript:
<body onselectstart="return false;">
通过CSS和JavaScript,我可以使所有流行浏览器中的所有内容都无法选择.但是,此代码也使区域无法选择,这是可用性较差的主要情况.我使用CSS使输入区域可选:
Through CSS and JavaScript, I can make all content unselectable across all popular browsers. BUT, this code also makes areas unselectable, which is a major case of poor usability. I use CSS to make input areas selectable:
-webkit-user-select: text;
-khtml-user-select: text;
-moz-user-select: text;
-o-user-select: text;
user-select: text;
..就像您可能期望的那样,它不涵盖Internet Explorer,因为我使用JavaScript禁止了所有内容的选择.
.. and as you might have expected, this does not cover Internet Explorer, since I used JavaScript to disable all content from being selectable.
我该怎么做才能使除输入区域之外的所有内容都无法选择?
What can I do to make all content unselectable except for input areas?
推荐答案
由于该事件冒泡到主体而不是起源于该主体,我认为您可以检查实际目标节点的节点名称,并避免为该目标返回false某些节点上发生的事件:
Since the event is bubbling up to the body and not originating there, I think you can check the node name for the actual target node, and avoid returning false for events occurring on certain nodes:
<body onselectstart="if ((event.target || event.srcElement).nodeName !== 'INPUT') return false;">
这篇关于使所有文字除& lt; input& gt;在Internet Explorer中无法选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!