如何禁用文本选择突出显示

How to disable text selection highlighting

提问人: 提问时间:5/6/2009 最后编辑:Mateen Ulhaq 更新时间:12/1/2022 访问量:2682533

问:

对于充当按钮(例如,此 Stack Overflow 页面侧边栏上标题为“问题标签和用户”的按钮)或选项卡的锚点,如果用户不小心选择了文本,是否有 CSS 标准方法可以禁用突出显示效果?

我意识到这可以用 JavaScript 完成,稍微谷歌搜索一下就会产生仅限 Mozilla 的选项。-moz-user-select

有没有一种符合标准的方法来用CSS来实现这一点,如果没有,“最佳实践”方法是什么?

CSS 跨浏览器 高亮显示 文本选择

评论

10赞 3/15/2011
Element Witch 中的元素是否可以禁用高亮显示,是否可以在 CSS 的 style 或 class 属性中启用高亮?或者换句话说,-webkit-user-select 等是否有其他值。除了没有?
10赞 JK. 5/17/2013
相关: stackoverflow.com/questions/16600479/... = 如何只允许选择某些子元素
14赞 DaAwesomeP 12/12/2014
在某些浏览器中存在一个错误,即执行“全选”(CTRL+A 和 CMD+A)仍然会选择内容。这可以通过透明的选择颜色来对抗:::selection { background: transparent; } ::-moz-selection { background: transparent; }
3赞 AmerllicA 12/6/2017
在2017年,使用和设置浏览器版本是更好的方式,然后让一切变得很酷。postcssautoprefixerpostcss
1赞 Peter Mortensen 11/24/2019
用户界面已更改。2019 年,所有提到的三个项目现在都在左上角的汉堡菜单中。“标签”和“用户”在那里,“问题现在被称为Stack Overflow”(前面有一个图标)。

答:

233赞 X-Istence 5/6/2009 #1

在 CSS 3 的 user-select 属性可用之前,基于 Gecko 的浏览器会支持您已经找到的属性。WebKit 和基于 Blink 的浏览器支持该属性。-moz-user-select-webkit-user-select

当然,在不使用 Gecko 渲染引擎的浏览器中不支持此功能。

没有符合“标准”的快速简便的方法可以做到这一点;使用 JavaScript 是一种选择。

真正的问题是,为什么你希望用户不能突出显示和复制和粘贴某些元素?我从来没有遇到过一次我不想让用户突出显示我网站的某个部分。我的几个朋友,在花了很多时间阅读和编写代码后,会使用突出显示功能来记住他们在页面上的位置,或者提供一个标记,以便他们的眼睛知道下一步要看哪里。

我唯一能看到这有用的地方是,如果您有表单按钮,如果用户复制并粘贴网站,则不应复制和粘贴这些按钮。

评论

0赞 Tim Kersten 11/4/2009
对于嵌入式设备,这可能是必需的。即使用浏览器呈现 UI 的设备。
36赞 Gordon Tucker 1/7/2010
需要这样做的另一个原因是按住 Shift 键单击以选择网格或表格中的多行。您不想突出显示文本,而是希望它选择行。
40赞 Marc Hughes 6/4/2014
高度互动的网络应用程序,具有大量拖放功能...意外突出显示是一个很大的可用性问题。
39赞 hbw 5/6/2009 #2

除了仅限 Mozilla 的属性之外,不,没有办法仅使用标准 CSS 禁用文本选择(截至目前)。

如果您注意到,Stack Overflow 不会禁用其导航按钮的文本选择,我建议在大多数情况下不要这样做,因为它会修改正常的选择行为并使其与用户的期望相冲突。

评论

0赞 X-Istence 5/6/2009
虽然我同意它改变了用户期望的行为,但对于位于此表单字段旁边的“添加评论”按钮之类的东西来说,这是有意义的......
0赞 5/6/2009
但这难道不会暴露不必要的实现细节吗?无法选择输入或按钮的文本。
0赞 hbw 5/6/2009
@anon:大多数用户可能不会尝试选择按钮的文本,因此在实践中,这并不重要。此外,为了做到这一点,他们必须开始在按钮外部进行选择——如果他们在按钮本身内部单击,则 onclick 处理程序将激活。另外,某些浏览器(例如Safari)实际上允许您选择普通按钮的文本...
8赞 Mnebuerquo 8/4/2013
如果您从聊天线程中选择一组评论,并且每条评论旁边都有一个赞成/反对按钮,那么最好选择没有其他内容的文本。这就是用户所期望或想要的。他不想在每条评论中复制/粘贴按钮标签。
2赞 Gigala 7/25/2014
例如,如果您双击一个按钮,而不是将您重定向到另一个页面,而是打开一个 div,该怎么办?然后,由于双击,按钮的文本将被选中!
121赞 seanmonstar 5/6/2009 #3

您可以在 Firefox 和 Safari 中执行此操作(Chrome 也是如此?

::selection { background: transparent; }
::-moz-selection { background: transparent; }

评论

131赞 Keithamus 2/2/2011
我不建议这样做,因为它实际上并不能解决问题;禁用文本选择 - 它只是隐藏它。这可能会导致糟糕的可用性,因为如果我在页面上拖动光标,我可能会在不知情的情况下选择任意文本。这可能会导致各种奇怪的可用性“错误”。
2赞 AvL 9/19/2013
不适用于具有透明区域的 PNG 图像:将始终选择浅蓝色...有什么解决方法吗?
208赞 Pekka 11/14/2009 #4

Internet Explorer 的 JavaScript 解决方案是:

onselectstart="return false;"

评论

61赞 Mathias Bynens 5/26/2010
别忘了!ondragstart
953赞 Tim Down 12/5/2010 #5

在大多数浏览器中,这可以使用 CSS 属性的专有变体来实现,这些变体最初在 CSS 3 中提出,然后在 CSS 3 中被放弃,现在在 CSS UI Level 4 中提出:user-select

*.unselectable {
   -moz-user-select: none;
   -khtml-user-select: none;
   -webkit-user-select: none;

   /*
     Introduced in Internet Explorer 10.
     See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
   */
   -ms-user-select: none;
   user-select: none;
}

对于 Internet Explorer < 10 和 Opera < 15,您需要使用您希望不可选择的元素的属性。您可以使用 HTML 中的属性进行设置:unselectable

<div id="foo" unselectable="on" class="unselectable">...</div>

遗憾的是,这个属性不是继承的,这意味着你必须在 .如果这是一个问题,你可以改用 JavaScript 对元素的后代递归执行此操作:<div>

function makeUnselectable(node) {
    if (node.nodeType == 1) {
        node.setAttribute("unselectable", "on");
    }
    var child = node.firstChild;
    while (child) {
        makeUnselectable(child);
        child = child.nextSibling;
    }
}

makeUnselectable(document.getElementById("foo"));

2014 年 4 月 30 日更新:每当向树中添加新元素时,都需要重新运行此树遍历,但从 @Han 的评论来看,可以通过添加设置事件目标的事件处理程序来避免这种情况。有关详细信息,请参阅 http://jsbin.com/yagekiji/1mousedownunselectable


这仍然没有涵盖所有的可能性。虽然不可能在不可选择的元素中启动选择,但在某些浏览器(例如 Internet Explorer 和 Firefox)中,仍然不可能阻止选择在不可选择的元素之前和之后开始和结束,而不会使整个文档不可选择。

评论

22赞 Chris Calo 1/27/2012
不要使用 class=“unselectable”,只需使用属性选择器 [unselectable=“on”] { ... }
8539赞 39 revs, 24 users 20%Blowsie #6

2017 年 1 月更新:

根据 Can I use,Safari 的 + -webkit-user-select 足以在所有主流浏览器中实现所需的行为。user-select


这些都是可用的正确 CSS 变体:

.noselect {
  -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Safari */
     -khtml-user-select: none; /* Konqueror HTML */
       -moz-user-select: none; /* Old versions of Firefox */
        -ms-user-select: none; /* Internet Explorer/Edge */
            user-select: none; /* Non-prefixed version, currently
                                  supported by Chrome, Edge, Opera and Firefox */
}
<p>
  Selectable text.
</p>
<p class="noselect">
  Unselectable text.
</p>


请注意,它正处于标准化过程中(目前处于 W3C 工作草案中)。它不能保证在任何地方都能工作,并且浏览器之间的实现可能存在差异。此外,浏览器将来可能会放弃对它的支持。user-select


更多信息可以在 Mozilla Developer Network 文档中找到。

此属性的值为 、 、 、 和 。nonetexttoggleelementelementsallinherit

评论

42赞 Blowsie 1/14/2011
不错的代码 molokoloco :D ,尽管我个人会远离使用它,因为有时您可能需要不同浏览器的不同值,并且它依赖于 JavaScript。创建一个类并将其添加到您的元素中,或者将 css 应用于样式表中的元素类型是非常无懈可击的。
67赞 Blowsie 3/21/2011
'user-select'- 值:无 |文 |切换 |元素 |元素 |全部 |继承 - w3.org/TR/2000/WD-css3-userint-20000216
372赞 Claudiu 9/5/2012
这太荒谬了!做同一件事有很多不同的方法。让我们为用户选择制定一个新标准。我们称之为.那么我们就不会有这些问题了。尽管为了向后兼容,我们也应该包括其他的。所以现在代码变成了 .啊,好多了。standard-user-select-webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; standard-user-select: none;
5赞 aderchox 7/16/2021
根据 caniuse 的说法,它似乎不再需要这些前缀了。
3赞 Tamás Sengel 10/30/2021
@aderchox 在这种情况下,caniuse.com 是错误的。我仍然需要在 iOS 15.1 上使用 Safari 的线路。-webkit-user-select: none;
157赞 Benjamin Crouzier 5/25/2011 #7

如果你想禁用除元素以外的所有内容的文本选择,你可以在CSS中执行此操作(注意允许在子元素中覆盖,这在其他浏览器中是允许的):<p>-moz-nonenone

* {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: -moz-none;
    -o-user-select: none;
    user-select: none;
}

p {
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -o-user-select: text;
    user-select: text;
}

评论

13赞 joshuadelange 7/8/2011
确保还将输入字段设置为可选字段:p, input { -webkit-user-select: text; -khtml-user-select: text; -moz-user-select: text; -o-user-select: text; user-select: text; }
12赞 Jason 11/12/2011
请非常谨慎地关闭除一项之外的所有代码上的浏览器 UI 预期。例如,列表项 <li /> 文本呢?
0赞 Kevin Fegan 12/25/2013
只是一个更新......根据 MDN 自 Firefox 21 以来的 MDN 和 是相同的。-moz-nonenone
2赞 T4NK3R 7/15/2014
为此,您可以分别添加 cursor:default 和 cursor:text:)
0赞 Hypersoft Systems 9/12/2019
炸弹。也就是说。结束。 [选择无序列表中的所有内容,并使其不可选择,而不是破坏整个视图树。谢谢你的教训。我的按钮列表看起来很棒,并且对屏幕点击和按下做出正确响应,而不是启动 IME(android 剪贴板小部件)。ul>* { -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: -moz-none; -o-user-select: none; user-select: none; }
91赞 Alex 9/21/2011 #8

WebKit 的解决方法:

/* Disable tap highlighting */
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);

我在 CardFlip 示例中找到了它。

评论

1赞 Clint Pachl 4/30/2015
代替 rgba 也适用于 Android 上的 Chrome 42。transparent
87赞 Tom Auger 11/12/2011 #9

我喜欢混合CSS + jQuery解决方案。

要使里面的所有元素都不可选择,请使用以下 CSS:<div class="draggable"></div>

.draggable {
    -webkit-user-select: none;
     -khtml-user-select: none;
       -moz-user-select: none;
        -ms-user-select: none;
         -o-user-select: none;
            user-select: none;
}

.draggable input {
    -webkit-user-select: text;
     -khtml-user-select: text;
       -moz-user-select: text;
         -o-user-select: text;
            user-select: text;
 }

然后,如果你使用的是 jQuery,请在块中添加以下内容:$(document).ready()

if (($.browser.msie && $.browser.version < 10) || $.browser.opera) $('.draggable').find(':not(input)').attr('unselectable', 'on');

我想你仍然希望任何输入元素都是可交互的,因此是伪选择器。如果你不在乎,你可以改用。:not()'*'

注意:Internet Explorer 9 可能不需要这个额外的 jQuery 部分,因此您可能希望在其中添加版本检查。

评论

6赞 mhenry1384 1/31/2013
使用 -ms-user-select: none;(对于 IE10)和您的 jQuery “if” 应该是这样的: if (($.browser.msie && $.browser.version < 10) || $.browser.opera)
0赞 Nicolas Thery 3/11/2013
小心男人!!要使它在 firefox 中可选,您必须使用-moz-user-select: Normal;
8赞 WynandB 3/15/2013
@mhenry1384 已从版本 1.3 起弃用,并在版本 1.9 中删除 - api.jquery.com/jQuery.browserjQuery.browser
0赞 Tom Auger 3/15/2013
@Wynand 好点子。但是,存在什么样的“特征检测”来确定要使用的 CSS 属性呢?
0赞 Aequanox 3/28/2013
@TomAuger您可以使用jQuery.support,它允许您检查单个功能: 链接
49赞 Codler 4/18/2012 #10

如果你使用的是 LessBootstrap,你可以这样写:

.user-select(none);
49赞 Gaurang 10/26/2012 #11
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;

*.unselectable {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;
   user-select: none;
}
<div id="foo" unselectable="on" class="unselectable">...</div>
function makeUnselectable(node) {
    if (node.nodeType == 1) {
        node.unselectable = true;
    }
    var child = node.firstChild;
    while (child) {
        makeUnselectable(child);
        child = child.nextSibling;
    }
}

makeUnselectable(document.getElementById("foo"));
-webkit-user-select: none;
-moz-user-select: none;
onselectstart="return false;"
::selection { 
    background: transparent; 
}

::-moz-selection { 
    background: transparent; 
}

* {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: -moz-none;
    -o-user-select: none;
    user-select: none;
}

p {
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -o-user-select: text;
    user-select: text;
}
<div class="draggable"></div>
.draggable {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -o-user-select: none;
    user-select: none;
}

.draggable input {
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -o-user-select: text;
    user-select: text;
 }
if ($.browser.msie)
    $('.draggable').find(':not(input)').attr('unselectable', 'on');
32赞 JIT1986 10/30/2012 #12

将此添加到要禁用文本选择的第一个 div 中:

onmousedown='return false;' 
onselectstart='return false;'
33赞 karthipan raj 4/3/2013 #13

如果也不需要颜色选择,这将很有用:

::-moz-selection { background:none; color:none; }
::selection { background:none; color:none; }

...所有其他浏览器修复。它将在 Internet Explorer 9 或更高版本中工作。

评论

1赞 yaakov 7/28/2016
也许可以这样。color: inherit;
0赞 Bariq Dharmawan 1/28/2018
是的,我喜欢它。根据 Mozilla 文档,它是 css 选择器级别 3
23赞 Automatico 4/10/2013 #14

这不是CSS,但值得一提的是:

jQuery UI禁用选择

$("your.selector").disableSelection();
78赞 Ale 5/1/2013 #15

.hidden:after {
    content: attr(data-txt);
}
<p class="hidden" data-txt="Some text you don't want to be selected"></p>

不过,这不是最好的方法。

评论

3赞 Toothbrush 5/8/2014
您也可以用作属性。title
7赞 pseudosavant 9/17/2014
这是一个非常有创意的解决方案。特别是如果它使用 title 属性,因为这对屏幕阅读器来说可能更好。
4赞 pseudosavant 9/17/2014
我试过了(JSBin),但它在IE中不起作用。不幸的是,较旧的 IE 是唯一不起作用的 IE。user-select
1赞 saricden 11/6/2018
这是一个很好的非 JS 替代方案,可以在 Chrome 中使用!棒!
0赞 SunshinyDoyle 9/17/2021
这就是我需要阻止实际选择的,而不仅仅是阻止显示选择。
66赞 Elad Shechter 12/23/2013 #16

此外,对于 Internet Explorer,还需要添加伪类焦点 (.ClassName:focus) 和 .outline-style: none

.ClassName,
.ClassName:focus {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    outline-style: none; /* Internet Explorer  */
}

评论

3赞 pseudosavant 9/17/2014
这在 IE 中确实有效,只要选择从具有类的元素开始。请参阅此 JSBinclassName
21赞 apocalypse 2/23/2014 #17

检查我的解决方案没有 JavaScript:

jsFiddle

li:hover {
    background-color: silver;
}
#id1:before {
    content: "File";
}
#id2:before {
    content: "Edit";
}
#id3:before {
    content: "View";
}
<ul>
    <li><a id="id1" href="www.w1.com"></a>
    <li><a id="id2" href="www.w2.com"></a>
    <li><a id="id3" href="www.w3.com"></a>
</ul>

应用了我的技术的弹出菜单:http://jsfiddle.net/y4Lac/2/

20赞 Suraj Naik 3/21/2014 #18

虽然这个伪元素出现在 CSS 选择器 Level 3 的草稿中,但它在候选推荐阶段被删除了,因为它的行为似乎没有得到充分说明,尤其是嵌套元素,并且没有实现互操作性。

在 ::selection 如何在嵌套元素上工作中对此进行了讨论。

尽管它是在浏览器中实现的,但您可以通过在选择时使用与选项卡设计相同的颜色和背景颜色(在您的情况下)来制造未选择文本的错觉。

普通CSS设计

p { color: white;  background: black; }

在选择时

p::-moz-selection { color: white;  background: black; }
p::selection      { color: white;  background: black; }

禁止用户选择文本将引发可用性问题。

评论

0赞 halfer 4/27/2014
这一定是 Netbeans 自动补全不知道我在说什么的原因!
37赞 r3wt 4/10/2014 #19

在某些浏览器中有效:

::selection{ background-color: transparent;}
::moz-selection{ background-color: transparent;}
::webkit-selection{ background-color: transparent;}

只需在选择器前面添加所需的元素/ID,用逗号分隔,不带空格,如下所示:

h1::selection,h2::selection,h3::selection,p::selection{ background-color: transparent;}
h1::moz-selection,h2::moz-selection,h3::moz-selection,p::moz-selection{ background-color: transparent;}
h1::webkit-selection,h2::webkit-selection,h3::webkit-selection,p::webkit-selection{ background-color: transparent;}

其他答案更好;这可能应该被视为最后的手段/包罗万象。

评论

4赞 Volker E. 9/30/2014
很少有事情可以确定,但这个解决方案绝对不适用于所有浏览器。
52赞 Beep.exe 5/19/2014 #20

对于那些在 Android 浏览器中使用触摸事件无法实现相同功能的用户,请使用:

html, body {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    -webkit-tap-highlight-color: transparent;
}
30赞 Luke Madhanga 11/7/2014 #21

注意:

正确答案是正确的,因为它会阻止您选择文本。但是,这并不妨碍您复制文本,正如我将在接下来的几张屏幕截图中显示的那样(截至 2014 年 11 月 7 日)。

Before we have selected anything

After we have selected

The numbers have been copied

正如你所看到的,我们无法选择数字,但我们能够复制它们。

测试在:UbuntuGoogle Chrome 38.0.2125.111。

评论

1赞 NHDaly 3/5/2016
我也有同样的问题。在 Mac Chrome 48.0.2564.116 和 Mac Safari 9.0.3 上。值得注意的是,Mac Firefox 43.0 不会复制该字符,而是在它们之间粘贴额外的结尾。对此应该怎么做?
27赞 SemanticZen 5/14/2015 #22

为了获得我需要的结果,我发现我必须同时使用 ::selectionuser-select

input.no-select:focus {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

input.no-select::selection {
    background: transparent;
}

input.no-select::-moz-selection {
    background: transparent;
}
139赞 ZECTBynmo 8/31/2015 #23

在上一个答案的解决方案中,选择已停止,但用户仍认为您可以选择文本,因为光标仍在更改。为了保持静态,你必须设置你的CSS光标:

.noselect {
    cursor: default;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
<p>
  Selectable text.
</p>
<p class="noselect">
  Unselectable text.
</p>

这将使您的文本完全平坦,就像在桌面应用程序中一样。

评论

0赞 kojow7 2/10/2018
“平坦”与什么相反?
0赞 Yeti 9/19/2018
@kojow7 与“分层”相对。而不是漂浮在其他元素之上的文本。它类似于SVG和PNG图像之间的区别。
4赞 Mentalist 5/24/2019
惊讶地发现 Firefox 在 2019 年仍然需要供应商前缀。我不顾一切地只使用,以为该标准现在会被采用,但遗憾的是它没有。让你想知道标准委员会的人还在争论什么。“不,你们......我真的认为这应该是因为它更具描述性,你知道吗?“我们已经结束了,迈克。我们将不得不省略撇号,这很糟糕!“够了,大家!我们将在下个月再次审议此事。标准委员会会议休会!user-select: none;user-select: cant;
10赞 mustafa-elhelbawy 1/20/2016 #24

尝试使用这个:

::selection {
    background: transparent;
}

如果您希望在特定元素中指定不选择,只需将元素类或 id 放在选择规则之前,例如:

.ClassNAME::selection {
    background: transparent;
}
#IdNAME::selection {
    background: transparent;
}

评论

2赞 Tiago Rangel 6/29/2021
这根本不会禁用选择...它只会使它不可见
37赞 Gaurav Aggarwal 3/28/2016 #25

假设有两个这样的:div

.second {
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
  /* Chrome/Safari/Opera */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* Internet Explorer/Edge */
  -webkit-touch-callout: none;
  /* iOS Safari */
}
<div class="first">
  This is my first div
</div>

<div class="second">
  This is my second div
</div>

将光标设置为默认值,以便给用户一种无法选择的感觉。

需要使用前缀在所有浏览器中支持它。如果没有前缀,这可能不适用于所有答案。

21赞 Mohammed Javed 4/1/2016 #26

我从CSS-Tricks网站上学到了东西。

user-select: none;

还有:

::selection {
    background-color: transparent;
}

::moz-selection {
    background-color: transparent;
}

::webkit-selection {
    background-color: transparent;
}

评论

1赞 Tiago Rangel 6/29/2021
它只会使它不可见
8赞 Pushp Singh 6/3/2016 #27

这种突出显示效果是由于称为悬停 (onMouseHover) 的操作造成的。

当您将鼠标悬停在任何选项卡上时,其颜色将发生变化。

举个例子,

HTML 代码

<div class="menu">
    <ul class="effect">
        <li>Questions</li>
        <li>JOBS</li>
        <li>Users</li>
    </ul>
</div>

CSS 代码

.effect:hover {
    color: none;
}

如果您想突出显示它,您可以使用任何颜色。否则,您可以使用 .none

评论

7赞 Flyout91 7/28/2017
不,你说的是悬停,意思是“当鼠标悬停在文本上时”。它与“选择文本”不同。默认情况下,悬停时没有颜色。
62赞 user1012506 8/28/2016 #28

尝试将这些行插入到 CSS 中,并在类属性中调用“disHighlight”:

.disHighlight {
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
    -o-user-select: none;
    -moz-user-select: none;
}
11赞 Chirag Dave 9/6/2016 #29

您可以使用如下属性...*-user-select

p {
    -webkit-user-select: none;  /* Chrome all and Safari all */
    -moz-user-select: none;     /* Firefox all */
    -ms-user-select: none;      /* Internet Explorer 10 and later */
    user-select: none;          /* Likely future */
}

详细说明的链接

0赞 grinmax 3/3/2017 #30

也许您可以通过以下方式使用此解决方案::before

nav li {
    display: inline-block;
    position: relative;
}

nav li a {
    display: inline-block;
    padding: 10px 20px;
}

nav li a:hover {
    color: red;
}

nav li.disabled:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
}
<nav>
    <ul>
    <li><a href="#">Link</a></li>
    <li class="disabled"><a href="#">Link</a></li>
    <li><a href="#">Link</a></li>
    </ul>
</nav>

JsFiddle - https://jsfiddle.net/grinmax_/9L1cckxu/

评论

5赞 codeWithMe 11/12/2018
这不是答案,您仍然可以选择文本。
80赞 Alireza 6/5/2017 #31

为此,您可以使用 CSSJavaScript

旧版浏览器(如版本的 Internet Explorer)支持 JavaScript 方式,但如果不是您的情况,请使用 CSS 方式:

HTML/JavaScript格式:

<html onselectstart='return false;'>
  <body>
    <h1>This is the Heading!</h1>
    <p>And I'm the text, I won't be selected if you select me.</p>
  </body>
</html>

HTML/CSS格式:

.not-selectable {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
<body class="not-selectable">
  <h1>This is the Heading!</h1>
  <p>And I'm the text, I won't be selected if you select me.</p>
</body>

5赞 Yevgeniy Afanasyev 1/31/2018 #32

更好的是,您可以禁用文本选择。

如果你喜欢 Sass (SCSS),并且不想使用 Compass,你可以这样做:

styles.scss

@mixin user-select($select) {
    -webkit-touch-callout:#{$select};
    @each $pre in -webkit-, -moz-, -ms-, -o-, -khtml- {
        #{$pre + user-select}: #{$select};
    }
    #{user-select}: #{$select};
}

.no-select {
    @include user-select(none);
}
4赞 omikes 2/2/2018 #33

我将各种浏览器 CSS 选择属性与 Internet Explorer < 9 所需的不可选择标签相结合。

<style>
[unselectable="on"] {
    -webkit-user-select: none; /* Safari */
    -moz-user-select: none; /* Firefox */
    -ms-user-select: none; /* Internet Explorer 10+/Edge */
    user-select: none; /* Standard */
}
</style>
<div unselectable="on">Unselectable Text</div>
61赞 Kazmi 3/31/2018 #34

快速黑客更新

如果将该值用于所有 CSS 属性(包括它的浏览器前缀),则仍可能出现问题。noneuser-select

.div {
    -webkit-user-select: none; /* Chrome all / Safari all */
    -moz-user-select: none;    /* Firefox all             */
    -ms-user-select: none;     /* Internet Explorer  10+  */
     user-select: none;        /* Likely future           */
}

正如 CSS-Tricks 所说,问题是

WebKit 仍然允许复制文本,前提是您选择文本周围的元素。

您还可以使用下面的方法来选择整个元素,这意味着如果您单击一个元素,则该元素中包含的所有文本都将被选中。为此,您所要做的就是将值更改为 。enforcenoneall

.force-select {
    -webkit-user-select: all;  /* Chrome 49+     */
    -moz-user-select: all;     /* Firefox 43+    */
    -ms-user-select: all;      /* No support yet */
    user-select: all;          /* Likely future  */
}
19赞 jasonleonhard 4/30/2018 #35

如果你想用CSS禁用整个页面的选择和高亮显示,你可以轻松地将其应用于所有元素:

* {
    -webkit-touch-callout: none; /* iOS Safari */
      -webkit-user-select: none; /* Safari */
       -khtml-user-select: none; /* Konqueror HTML */
         -moz-user-select: none; /* Firefox */
          -ms-user-select: none; /* Internet Explorer/Edge */
              user-select: none; /* Non-prefixed version, currently
                                    supported by Chrome and Opera */
}
8赞 Abrar Jahin 9/22/2018 #36

使用 CSS-

div {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -o-user-select: none;

  "unselectable='on' onselectstart="return false;"
  onmousedown="return false;
}
<div>
  Blabla
  <br/> More Blabla
  <br/> More Blabla...
</div>

1赞 Navneet Kumar 10/31/2018 #37
::selection {background: transparent; color: transparent;}

::-moz-selection {background: transparent; color: transparent;}

评论

1赞 Nico Haase 10/31/2018
你能进一步解释一下你的意思吗?这如何改善其他高投票率的答案?
3赞 codeWithMe 11/21/2018
这不会禁用文本选择,它所做的只是为用户隐藏它。计算机本身仍然知道它正在被选中,背景颜色只是透明的。但是,如果您按 ctr(mac 上的 cmd)+ A,然后复制,您可以将文本粘贴到其他地方。
0赞 Peter Mortensen 5/10/2019
解释是有序的。
28赞 codeWithMe 11/8/2018 #38

这很容易做到:

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

或者:

假设您有一个 .你将不得不删除它的innerHTML ,在本例中为Hello, World。然后,您必须转到CSS并执行此操作:<h1 id="example">Hello, World!</h1>h1

#example::before // You can of course use **::after** as well.
{
    content: 'Hello, World!'; // Both single-quotes and double-quotes can be used here.

    display: block; // To make sure it works fine in every browser.
}

现在它只是认为它是一个块元素,而不是文本。

0赞 GeekyMonkey 8/27/2019 #39

您可能还希望防止在触摸阻止选择的元素(如按钮)时出现上下文菜单。为此,请将以下代码添加到整个页面,或仅将这些按钮元素添加到其中:

$("body").on("contextmenu",function(e){
    return false;
});
55赞 Alessandro_Russo 10/11/2019 #40

使用 SASS(SCSS 语法)

你可以用 mixin 来做到这一点:

// Disable selection
@mixin disable-selection {
    -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none;   /* Safari */
    -khtml-user-select: none;    /* Konqueror HTML */
    -moz-user-select: none;      /* Firefox */
    -ms-user-select: none;       /* Internet Explorer/Edge */
    user-select: none;           /* Non-prefixed version, currently supported by Chrome and Opera */
}

// No selectable element
.no-selectable {
    @include disable-selection;
}

在 HTML 标记中:

<div class="no-selectable">TRY TO HIGHLIGHT. YOU CANNOT!</div>

在此 CodePen尝试一下

如果您使用的是自动前缀,则可以删除其他前缀

浏览器兼容性在这里

-2赞 Murtaza JAFARI 1/17/2020 #41

这可能有效

    ::selection {
        color: none;
        background: none;
    }
    /* For Mozilla Firefox */
    ::-moz-selection {
        color: none;
        background: none;
    }

评论

10赞 Jason Aller 1/17/2020
当将十年前的答案与四十四个其他答案相加时,门槛更高。在你的回答中说明你的答案解决了问题的哪些新方面,以及随着时间的推移,新技术的出现如何影响了答案,这是很有用的。答案还应该解释代码是如何以及做什么的。
9赞 Santiago Cabrera 3/6/2020 #42

向 CSS 添加一个类,该类定义您不能选择或突出显示元素。我举个例子:

<style> 
    .no_highlighting{
        user-select: none;
    }

    .anchor_without_decoration:hover{
        text-decoration-style: none;
    }
</style>

<a href="#" class="anchor_without_decoration no_highlighting">Anchor text</a>
15赞 Rajilesh Panoli 9/3/2020 #43

你试过这个吗?

.disableSelect{
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
    -o-user-select: none;
    -moz-user-select: none;

    pointer-events:none;
}

评论

0赞 Matias Haeussler 10/18/2021
这是禁用文本输入选择所必需的。 必须位于 div 类和 input 类user-select: nonepointer-events: none
0赞 robm 7/17/2023
pointer-events 是我需要的秘密,以阻止选择非技术背景的图像
6赞 Shubham Garg 7/10/2021 #44

我看到许多详细的答案,但我相信只写这行代码就足以完成所需的任务:

*{
    -webkit-user-select: none;
 }
21赞 3 revsRixTheTyrunt #45

第一种方法:(完全是胡说八道):

.no-select::selection, .no-select *::selection {
  background-color: Transparent;
}

.no-select { /* Sometimes I add this too. */
  cursor: default;
}
<span>RixTheTyrunt is da best!</span>
<br>
<span class="no-select">RixTheTyrunt is da best!</span>

片段:

.no-select::selection, .no-select *::selection {
  background-color: Transparent;
}

.no-select {
  /* Sometimes I add this too. */
  cursor: default;
}
<span>RixTheTyrunt is da best!</span>
<br>
<span class="no-select">RixTheTyrunt is da best!</span>

第二种方法(不包括废话)

.no-select {
  user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
}

片段:

.no-select {
  user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
}
<span>RixTheTyrunt is da best!</span>
<br>
<span class="no-select">RixTheTyrunt is da best!</span>

首先,解决问题。然后,编写代码。

约翰·约翰逊