使所有文字除< input>在Internet Explorer中无法选择? [英] Make all text EXCEPT <input> unselectable in Internet Explorer?

查看:92
本文介绍了使所有文字除< input>在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;

..正如您所料,由于我使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆