对于像按钮一样运作的插槽(例如,这个 Stack Overflow 页面侧栏上的按钮题为 问题、标签和用户)或选项卡,如果用户随机选择文本,则是否有 CSS 标准方式禁用突出效果?

我意识到可以用JavaScript做到这一点,而谷歌有点带来了Mozilla-only -moz-user-select选项。

有没有一个符合标准的方式来实现这一点与CSS,如果没有,什么是“最好的实践”方法?


当前回答

如果您正在使用 Less 和 Bootstrap 您可以写下:

.user-select(none);

其他回答

假设有两种类似的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"> 这是我的第一个 div </div> <div class="second"> 这是我的第二个 div </div>

设置默认的路由器,以便它给用户一个不可选择的感觉。

预定必须用于支持它在所有浏览器. 没有预定,这可能不会在所有答案工作。

这在某些浏览器中工作:

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

简单地将您所需的元素/ID 添加到由无空间的 commas 分开的选择器前面,如下:

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;}

其他答案是更好的;这可能应该被视为最后的避难所 / 冲突。

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

onmousedown='return false;' 
onselectstart='return false;'

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

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

风格.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);
}

这不是CSS,但值得一提:

jQuery UI 无法选择:

$("your.selector").disableSelection();