Html 哪个 css 样式处理按钮上的点击事件?除了悬停

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3945267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:51:15  来源:igfitidea点击:

which css style handles the click event on a button? other than hover

htmlcss

提问by Blankman

I see this site that has a button, when I hover over it the background changes.

我看到这个网站有一个按钮,当我将鼠标悬停在它上面时,背景会发生变化。

When I click on the button, the button shading inverts i.e. reacts to the click event.

当我点击按钮时,按钮底纹反转,即对点击事件做出反应。

Which CSS style is this? I know :hover is for hover, but what about a click?

这是哪种 CSS 样式?我知道 :hover 用于悬停,但是单击呢?

采纳答案by Diodeus - James MacFarlane

CSS does not handle events.

CSS 不处理事件。

You're looking for the :activepseudo class, which is only for the A-tag, not buttons. You can style A tags to look like buttons as well though.

您正在寻找:active伪类,它仅适用于 A 标签,而不适用于按钮。不过,您也可以将 A 标签的样式设置为看起来像按钮。

See: http://w3schools.com/CSS/css_pseudo_classes.asp

见:http: //w3schools.com/CSS/css_pseudo_classes.asp

回答by Kir Kanos

CSS3 does handle activation events now (2013).

CSS3 现在确实处理激活事件(2013)。

The :active pseudo-class is working on any css selector like BUTTONs, not just A tags.

:active 伪类适用于任何 css 选择器,例如 BUTTON,而不仅仅是 A 标签。

CSS3 STYLE

CSS3 样式

p { margin: 2em; }
button { background-color: White; color: SteelBlue; }
button:hover { background-color: LightSteelBlue; }
button:active { background-color: SteelBlue; color: White; }

HTML5

HTML5

    <p>
        <button>test button with :active pseudo-class</button>
    </p>

Demo: http://jsfiddle.net/wp86u/

演示:http: //jsfiddle.net/wp86u/

See: http://www.w3schools.com/css/css_pseudo_classes.asp(content changed since Diodeus answer)

请参阅:http: //www.w3schools.com/css/css_pseudo_classes.asp(自 Diodeus 回答后内容已更改)

回答by Chris Cox

What you're looking at is the browser's default styles for the :active pseudo-class, which also applies to keyboard input on a button (try tabbing to it and pressing the spacebar).

您正在查看的是 :active 伪类的浏览器默认样式,这也适用于按钮上的键盘输入(尝试使用 Tab 键并按空格键)。

These pseudo-classes do NOT just apply to the anchor element, it's a common misconception caused by old versions of IE not supporting them on anything other than the anchor element.

这些伪类不仅仅适用于锚元素,这是由于旧版本的 IE 不支持锚元素以外的任何东西而引起的常见误解。