如何禁用文本选择突出显示
对于作用类似于按钮的锚点(例如,此 StackOverflow 页面侧边栏上标题为问题、标签和用户的按钮)或者选项卡,是否有 CSS 标准方法可以在用户意外选择文本时禁用突出显示效果?
我意识到这可以通过 JavaScript 来完成,并且通过谷歌搜索得到了 Mozilla 独有的 -moz-user-select
选项。
是否有一种符合标准的方法可以使用 CSS 来实现此目的,如果没有,“最佳实践”方法是什么?
For anchors that act like buttons (for example, the buttons on the sidebar of this Stack Overflow page titled Questions, Tags, and Users) or tabs, is there a CSS standard way to disable the highlighting effect if the user accidentally selects the text?
I realize that this could be done with JavaScript and a little googling yielded the Mozilla-only -moz-user-select
option.
Is there a standard-compliant way to accomplish this with CSS, and if not, what is the "best practice" approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
2017 年 1 月更新:
根据我可以使用,用于 Safari 的
user-select
+-webkit-user-select
足以在所有主要浏览器中实现所需的行为。这些是所有可用的正确 CSS 变体:
请注意,
user-select
正在标准化过程中(目前处于 W3C 工作草案)。 它不能保证在所有地方都能工作,并且浏览器之间的实现可能存在差异。 此外,浏览器将来可能会放弃对其的支持。更多信息请参阅 Mozilla 开发者网络文档。
该属性的值为
none
、text
、toggle
、element
、elements
、全部
和继承
。UPDATE January, 2017:
According to Can I use, the
user-select
+-webkit-user-select
for Safari is enough to achieve desired behavior in all major browsers.These are all of the available correct CSS variations:
Note that
user-select
is in standardization process (currently in a W3C working draft). It is not guaranteed to work everywhere and there might be differences in implementation among browsers. Also, browsers can drop support for it in the future.More information can be found in Mozilla Developer Network documentation.
The values of this attribute are
none
,text
,toggle
,element
,elements
,all
andinherit
.在大多数浏览器中,这可以使用 CSS
user-select
属性的专有变体来实现,最初在 CSS 中提出,然后在 CSS 3 中被放弃,现在在 CSS UI 级别 4:对于 Internet Explorer < 10 和 Opera < 15、您将需要使用您希望不可选择的元素的
unselectable
属性。 您可以使用 HTML 中的属性来设置它:遗憾的是,该属性不是继承的,这意味着您必须将属性放在
内每个元素的开始标记中。 如果这是一个问题,您可以使用 JavaScript 对元素的后代递归执行此操作:
2014 年 4 月 30 日更新:每当将新元素添加到树中时,都需要重新运行此树遍历,但是从@Han的评论看来,可以通过添加一个在事件目标上设置
unselectable
的mousedown
事件处理程序来避免这种情况。 有关详细信息,请参阅 http://jsbin.com/yagekiji/1。这仍然没有涵盖所有可能性。 虽然不可能在不可选择的元素中启动选择,但在某些浏览器(例如 Internet Explorer 和 Firefox)中,仍然不可能阻止在不可选择的元素之前开始和之后结束的选择,而不使整个文档变得不可选择。
In most browsers, this can be achieved using proprietary variations on the CSS
user-select
property, originally proposed and then abandoned in CSS 3 and now proposed in CSS UI Level 4:For Internet Explorer < 10 and Opera < 15, you will need to use the
unselectable
attribute of the element you wish to be unselectable. You can set this using an attribute in HTML:Sadly this property isn't inherited, meaning you have to put an attribute in the start tag of every element inside the
<div>
. If this is a problem, you could instead use JavaScript to do this recursively for an element's descendants:Update 30 April 2014: This tree traversal needs to be rerun whenever a new element is added to the tree, but it seems from a comment by @Han that it is possible to avoid this by adding a
mousedown
event handler that setsunselectable
on the target of the event. See http://jsbin.com/yagekiji/1 for details.This still doesn't cover all possibilities. While it is impossible to initiate selections in unselectable elements, in some browsers (Internet Explorer and Firefox, for example) it's still impossible to prevent selections that start before and end after the unselectable element without making the whole document unselectable.
直到 CSS 3 的 user-select 属性可用, 基于 Gecko 的浏览器支持
-moz-user-select< /code> 您已经找到的属性。 WebKit 和基于 Blink 的浏览器支持
-webkit-user-select
财产。当然,不使用 Gecko 渲染引擎的浏览器不支持这一点。
没有符合“标准”的快速简便的方法来做到这一点; 使用 JavaScript 是一种选择。
真正的问题是,为什么您希望用户无法突出显示并且无法复制和粘贴某些元素? 我从来没有遇到过不想让用户突出显示我网站的某个部分的情况。 我的几个朋友在花费大量时间阅读和编写代码后,会使用突出显示功能来记住他们在页面上的位置,或者提供一个标记,以便他们的眼睛知道下一步该看哪里。
我认为这是有用的唯一地方是,如果您有表单按钮,如果用户复制并粘贴网站,则不应复制和粘贴这些按钮。
Until CSS 3's user-select property becomes available, Gecko-based browsers support the
-moz-user-select
property you already found. WebKit and Blink-based browsers support the-webkit-user-select
property.This of course is not supported in browsers that do not use the Gecko rendering engine.
There is no "standards" compliant quick-and-easy way to do it; using JavaScript is an option.
The real question is, why do you want users to not be able to highlight and presumably copy and paste certain elements? I have not come across a single time that I wanted to not let users highlight a certain portion of my website. Several of my friends, after spending many hours reading and writing code will use the highlight feature as a way to remember where on the page they were, or providing a marker so that their eyes know where to look next.
The only place I could see this being useful is if you have buttons for forms that should not be copy and pasted if a user copy and pasted the website.
Internet Explorer 的 JavaScript 解决方案是:
A JavaScript solution for Internet Explorer is:
如果您想禁用除
元素之外的所有内容的文本选择,您可以在 CSS 中执行此操作(注意
-moz-none
,它允许覆盖子元素,在其他浏览器中允许使用none
):If you want to disable text selection on everything except on
<p>
elements, you can do this in CSS (watch out for the-moz-none
which allows override in sub-elements, which is allowed in other browsers withnone
):在前面答案的解决方案中,选择已停止,但用户仍然认为您可以选择文本,因为光标仍然发生变化。 为了保持静态,你必须设置 CSS 光标:
这将使您的文本完全平坦,就像在桌面应用程序中一样。
In the solutions in previous answers selection is stopped, but the user still thinks you can select text because the cursor still changes. To keep it static, you'll have to set your CSS cursor:
This will make your text totally flat, like it would be in a desktop application.
您可以在 Firefox 和 Safari 中执行此操作(Chrome 也可以吗?)
You can do so in Firefox and Safari (Chrome also?)
WebKit 的解决方法:
我在 CardFlip 示例中找到了它。
Workaround for WebKit:
I found it in a CardFlip example.
我喜欢 CSS + jQuery 的混合解决方案。
要使
内的所有元素不可选择,请使用此 CSS:
然后,如果您使用 jQuery,请将其添加到
中$(document).ready() 块:
我认为您仍然希望任何输入元素都是可交互的,因此使用
:not()
伪选择器。 如果您不介意的话,可以使用'*'
来代替。警告:Internet Explorer 9 可能不需要这个额外的 jQuery 部分,因此您可能需要在其中添加版本检查。
I like the hybrid CSS + jQuery solution.
To make all elements inside
<div class="draggable"></div>
unselectable, use this CSS:And then, if you're using jQuery, add this inside a
$(document).ready()
block:I figure you still want any input elements to be interactable, hence the
:not()
pseudo-selector. You could use'*'
instead if you don't care.Caveat: Internet Explorer 9 may not need this extra jQuery piece, so you may want to add a version check in there.
您可以使用 CSS 或 JavaScript 来实现此目的。
JavaScript 方式在旧浏览器中受支持,例如旧版本的 Internet Explorer,但如果不是您的情况,请使用 CSS那么方式:
HTML/JavaScript:
HTML/CSS:
You can use CSS or JavaScript for that.
The JavaScript way is supported in older browsers, like old versions of Internet Explorer as well, but if it's not your case, use the CSS way then:
HTML/JavaScript:
HTML/CSS:
但这不是最好的方法。
It's not the best way, though.
另外对于 Internet Explorer,您需要添加伪类
focus
(.ClassName:focus) 和outline-style: none
。For Internet Explorer in addition, you need to add pseudo class
focus
(.ClassName:focus) andoutline-style: none
.尝试将这些行插入 CSS 并在类属性中调用“disHighlight”:
Try to insert these rows into the CSS and call the "disHighlight" at class property:
快速破解更新
如果您对所有 CSS
user-select
属性(包括其浏览器前缀)使用值none
,则会出现这仍然可能发生的问题。正如 CSS-Tricks 所说,问题 是:
如果您选择文本周围的元素,WebKit 仍然允许复制文本。
您还可以使用下面的方法来
强制
选择整个元素,这意味着如果您单击某个元素,则该元素中包含的所有文本都将被选择。 为此,您所需要做的就是将值none
更改为all
。A Quick Hack Update
If you use the value
none
for all the CSSuser-select
properties (including browser prefixes of it), there is a problem which can be still occurred by this.As CSS-Tricks says, the problem is:
WebKit still allows the text to be copied, if you select elements around it.
You can also use the below one to
enforce
that an entire element gets selected which means if you click on an element, all the text wrapped in that element will get selected. For this all you have to do is changing the valuenone
toall
.使用 SASS (SCSS 语法),
您可以使用 mixin:
在 HTML 标记中:
在此 CodePen。
如果您使用autoprefixer,您可以删除其他前缀。
浏览器兼容性< a href="https://caniuse.com/#feat=user-select-none" rel="nofollow noreferrer">此处。
With SASS (SCSS syntax)
You can do this with a mixin:
In an HTML tag:
Try it in this CodePen.
If you are using an autoprefixer you can remove other prefixes.
Browser compatibility here.
对于那些在 Android 浏览器中无法通过触摸事件实现相同目标的人,请使用:
For those who have trouble achieving the same in the Android browser with the touch event, use:
如果您使用的是 Less 和 Bootstrap 你可以写:
If you are using Less and Bootstrap you could write:
除了 Mozilla 独有的属性之外,没有办法仅使用标准 CSS 来禁用文本选择(截至目前)。
如果您注意到,Stack Overflow 不会禁用其导航按钮的文本选择,我建议在大多数情况下不要这样做,因为它会修改正常的选择行为并使其与用户的期望发生冲突。
Aside from the Mozilla-only property, no, there is no way to disable text selection with just standard CSS (as of now).
If you notice, Stack Overflow doesn't disable text selection for their navigation buttons, and I would recommend against doing so in most cases, since it modifies normal selection behavior and makes it conflict with a user's expectations.
这适用于某些浏览器:
只需在选择器前面添加所需的元素/ID,用逗号分隔,不带空格,如下所示:
其他答案更好; 这可能应该被视为最后的手段/万能的。
This works in some browsers:
Simply add your desired elements/ids in front of the selectors separated by commas without spaces, like so:
The other answers are better; this should probably be seen as a last resort/catchall.
假设有两个像这样的
div
:将光标设置为默认值,这样会给用户一种无法选择的感觉。
Suppose there are two
div
s like this:Set cursor to default so that it will give a unselectable feel to the user.
如果也不需要颜色选择,这将很有用:
...所有其他浏览器修复。 它将在 Internet Explorer 9 或更高版本中运行。
This will be useful if color selection is also not needed:
...all other browser fixes. It will work in Internet Explorer 9 or later.
将其添加到要禁用文本选择的第一个 div:
Add this to the first div in which you want to disable the selection for text:
注意:
正确答案是正确的,因为它阻止您选择文本。 但是,它不会阻止您复制文本,正如我将通过接下来的几个屏幕截图(截至 2014 年 11 月 7 日)所示。
如您所见,我们无法选择数字,但是我们能够复制它们。
测试环境:Ubuntu、谷歌浏览器 38.0.2125.111。
NOTE:
The correct answer is correct in that it prevents you from being able to select the text. However, it does not prevent you from being able to copy the text, as I'll show with the next couple of screenshots (as of 7th Nov 2014).
As you can see, we were unable to select the numbers, but we were able to copy them.
Tested on: Ubuntu, Google Chrome 38.0.2125.111.
这很容易完成:
或者:
假设您有一个
Hello, World!
。 您必须删除该
h1
的innerHTML,在本例中为Hello, World。 然后你必须转到 CSS 并执行以下操作:现在它只是认为它是一个块元素,而不是文本。
It is easily done with:
Alternatively:
Let's say you have a
<h1 id="example">Hello, World!</h1>
. You will have to remove the innerHTML of thath1
, in this case Hello, World. Then you will have to go to CSS and do this:Now it simply thinks it is a block-element, and not text.
为了获得我需要的结果,我发现我必须同时使用
::selection
和user-select
To get the result I needed, I found I had to use both
::selection
anduser-select
这不是 CSS,但值得一提:
jQuery UI 禁用选择:
This is not CSS, but it is worth a mention:
jQuery UI Disable Selection:
检查我的没有 JavaScript 的解决方案:
jsFiddle
应用我的技术的弹出菜单: http://jsfiddle.net/y4Lac/2/
Check my solution without JavaScript:
jsFiddle
Popup menu with my technique applied: http://jsfiddle.net/y4Lac/2/
我是从 CSS-Tricks 网站学习的。
这还有:
I have learned from the CSS-Tricks website.
And this also:
第一种方法:(完全无意义):
片段:
第二种方法(不包括任何废话)
片段:
FIRST METHOD: ( TOTALLY NONSENSE ):
Snippet:
SECOND METHOD ( NO NONSENSE INCLUDED )
Snippet: