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

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

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


当前回答

雖然這個假設元素在CSS Selectors Level 3的草案中,但在候選人推薦階段中被移除,因為它顯然他的行為被低估,特別是與<unk>化元素,並且互動性未達成。

它正在讨论在如何 :: 选择工作在无缝元素。

尽管它正在在浏览器中实施,但您可以通过使用相同的颜色和背景颜色来创建一个不被选择的文本的幻觉,与选项卡设计(在您的情况下)。

正常的CSS设计

p { color: white;  background: black; }

关于选择

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

不允许用户选择文本会引发可用性问题。

其他回答

第一個方法:(完全無情):

.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 {背景颜色:透明; }.no-select { /* 有时我也添加了这个。 */ cursor:默认; } <span>RixTheTyrunt是最好的!</span> <br> <span class="no-select">RixTheTyrunt是最好的!</span>

第二种方法(不含)

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

狙击:

.no-select { 用户选择: 无; -moz-用户选择: 无; -webkit-用户选择: 无; } <span>RixTheTyrunt 是最好的!</span> <br> <span class="no-select">RixTheTyrunt 是最好的!</span>

首先,解决问题,然后写代码,约翰·约翰逊

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

吉普赛

首頁 〉外文書 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學 〉文學

使用我的技术的Popup菜单: http://jsfiddle.net/y4Lac/2/

对于 Internet Explorer 此外,您还需要添加 pseudo class focus (.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  */
}

除了Mozilla唯一的属性,不,没有办法用标准CSS(如现在)禁用文本选项。

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

关于 WebKit 的文章:

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

我在 CardFlip 例子中发现了它。