html - 为什么单击 div 会检查隐藏的单选按钮?

标签 html css radio-button label hidden

下面的 CSS/HTML 完全符合我的要求,但我不知道为什么。

<html>
<head>
<style>
input[type=radio] { display: none }
label > div { background: yellow }
label > div:hover { background: pink }
input[type=radio]:checked + div { background: red }
</style>
</head>
<body>
<label><input type="radio" name="r"><div>one</div></label>
<label><input type="radio" name="r"><div>two</div></label>
<label><input type="radio" name="r"><div>three</div></label>
</body>
</html>

我已经尝试了三种浏览器,它们都接受在 div 中的任意位置单击以检查其隐藏的单选按钮。

任何人都可以解释这个非常有用但可能没有记录的怪癖吗?

最佳答案

我刚刚找到了我自己问题的答案。

这是因为在“label”中单击控制单选按钮。请参阅:https://www.w3schools.com/tags/tag_label.asp

我将把这个问题留在这里,因为其他人可能会发现这个相对节俭的代码很有用。

关于html - 为什么单击 div 会检查隐藏的单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49697387/

相关文章:

jquery - 当我将鼠标悬停到表格行时,如何在 div 中显示表格中的图像?

javascript - 停止 div table-cell 在中心调整大小?

javascript - Angular : Correctly show which radio is selected initially

不需要 WordPress 联系表 7 radio

java - 在 Ajax 行为中获取单选按钮选择值

html - 流体高度受限图像 HTML

html - float 右 div 间歇性地推出对齐 - angularjs 模板问题?

javascript - 使用左右箭头导航到 rel=next 和 rel=prev 页面

html - 将最后一个列表项推到底部

html - 在 html/css 中查看图片上的 h1 标签