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

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

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


当前回答

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

吉普赛

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

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

其他回答

尝试将这些行插入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;
}

对于 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  */
}

对于那些在 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;
}

在大多数浏览器中,可以通过使用用户选择的CSS属性的属性变量来实现这一点,最初提出,然后在CSS 3中被遗弃,现在在CSS UI级别4中提出。

*.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 中,您需要使用您想要不可选择的元素的不可选择属性。

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

不幸的是,这个属性没有继承,这意味着你必须在 <div> 中每个元素的起点标签中放置一个属性,如果这是一个问题,你可以使用JavaScript以重复为元素的后代:

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"));


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

试着使用这个:

::selection {
    background: transparent;
}

如果您想指定不在特定元素中选择,则只需在选择规则之前设置元素类或ID,例如:

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