为什么CSS中的相应标签会触发输入的悬停?
我是否遗漏了某些内容,或者此示例的行为 - http://dabblet.com/result/gist/1716833 -在Webkits/Fx中比较奇怪?
有一个带有标签的输入和以下选择器:
input:hover + .target {
background: red;
}
当您将鼠标悬停附加到 input
的 label
时,不仅会触发 input
,还会触发此样式> 本身。更重要的是:带有 for
的 label
和包裹在 label
中的 input
之间存在差异 - 如果您'首先将鼠标悬停在 input
上,然后将光标直接移动到 .target
- 奇怪的悬停在包装版本中不会触发。
这只能在 Firefox 和 Safari/Chrome 中重现,但在 Opera 中没问题。
所以,问题是:规范中是否描述了这个问题?我找不到任何合适的地方来描述它并告诉我们什么行为是正确的。
Am I missing something, or the behavior of this example — http://dabblet.com/result/gist/1716833 — is rather strange in Webkits/Fx?
There is an input with label and the following selector:
input:hover + .target {
background: red;
}
And this style is triggered when you hover the label
attached to the input
, not only the input
itself. Even more: there is a difference between the label
with for
and input
wrapped in a label
— if you'd hover the input
at first and then move the cursor straight to the .target
— the strange hover won't trigger in wrapped version.
And this is only reproduces in Firefox and Safari/Chrome, but in Opera it's ok.
So, the questions is: if this issue is described somewhere in specs? I couldn't find any appropriate place that describes it and tells what behavior is right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在这已包含在 HTML 规范中;直到2012 年 10 月 WD 它已添加到 W3C HTML5(强调我的):
相同的文本出现在 生活规范。
几年前,我在网站联系表单的先前设计中发现了这种行为,其中
label:hover
还会在任何表单输入元素(无论是其表单输入元素)上触发:hover
后代或由其for
属性引用。此行为实际上已添加到此错误报告<的最新版本的 Gecko(Firefox 布局引擎)中/a> 以及 这个(相当短的)邮件列表线程,它是在 WebKit 中实现的 很多年前。正如您所注意到的,这种行为不会在 Opera 中重现; Opera Software 和微软似乎没有收到这份备忘录。
我在规范中可以找到的所有可能与此行为相关的内容是
但我可以得出的结论是,这种行为至少在 Gecko 和 WebKit 中是设计使然的。
关于你在这里所说的:
鉴于上述行为,唯一的可能性是您只是被级联咬伤了。
基本上,这条规则:
比这条规则更具体:
因此,在适用的浏览器中,第一个复选框的
label.target
在悬停时将始终为红色,因为更具体的规则始终优先。第二个复选框后面跟着一个span.target
,因此此行为均不适用;当光标位于span.target
上时,只有第二条规则才能生效。This is in the HTML spec now; it wasn't until the October 2012 WD that it was added to W3C HTML5 (emphasis mine):
Identical text appears in the living spec.
I discovered this very behavior a few years ago on the previous design of my site's contact form, where
label:hover
also triggers:hover
on any form input element that is either its descendant or referenced by itsfor
attribute.This behavior was actually added to a recent build of Gecko (Firefox's layout engine) in this bug report along with this (rather short) mailing list thread, and it was implemented in WebKit many years back. As you note, the behavior doesn't reproduce in Opera; it looks like Opera Software and Microsoft didn't get the memo.
All I can find in the spec that could relate to this behavior somehow is here, but I don't know for sure (italicized note by me):
But what I can conclude is that this behavior is by design in at least Gecko and WebKit.
Regarding what you state here:
Given the above behavior, the only possibility left here is that you've simply been bitten by the cascade.
Basically, this rule:
Is more specific than this rule:
So in applicable browsers, the
label.target
by your first checkbox will always be red on hover, because the more specific rule always takes precedence. The second checkbox is followed by aspan.target
, so none of this behavior applies; only the second rule can take effect while the cursor is over thespan.target
.