简体   繁体   English

提供for…id时,不要将鼠标悬停在输入标签上,无法激活输入的悬停样式。

[英]Stop hovering over input label from activing the input's hover styles when for…id is provided

I'm not sure if all browsers do this but if I provide a for attribute on a label that corresponds to an input's id , this makes hovering over the label trigger the input's hover styles which is undesirable in my case. 我不确定是否所有浏览器都这样做,但是如果我在标签上提供了与输入id相对应的for属性,这会使鼠标悬停在标签上会触发输入的悬停样式,这在我的情况下是不希望的。 Is there any way to stop this behavior while still having a form accessible to screen readers? 有什么方法可以阻止这种行为,同时仍使屏幕阅读器可以访问表单?

 .field label { display: block; margin: 0 0 0.5em 0; } .field input { background: white; border: 1px solid #999; border-radius: 0px; color: black; display: inline-block; padding: 0.5em 0.7em } .field input:hover { background: #efefff; border-color: #333; } 
 <div class="field"> <label for="myInput">Hover over this label:</label> <input id="myInput" type="text"> </div> 

You can use pointer-events:none; 您可以使用pointer-events:none; to disable hover effect for any element. 禁用任何元素的悬停效果。

 .field label { display: block; margin: 0 0 0.5em 0; pointer-events:none; } .field input { background: white; border: 1px solid #999; border-radius: 0px; color: black; display: inline-block; padding: 0.5em 0.7em } .field input:hover { background: #efefff; border-color: #333; } 
 <div class="field"> <label for="myInput">Hover over this label:</label> <input id="myInput" type="text"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM