CSS selectors - how to select #39;for#39; in CSS?(CSS 选择器 - 如何在 CSS 中选择“for?)
问题描述
我正在使用 jQuery 验证.
I'm using jQuery validation.
当 for 现在有效时,验证器正在创建这些:
When for is now valid, validator is creating those:
<label class="error" for="username" generated="true">
由于所有标签都具有相同的 class=error,我可以根据 'for' 使用 CSS 选择这个确切的标签吗?
As all label has got the same class=error can I select with CSS this exact one based on 'for'?
我知道如何用 jQuery 来解决这个问题——但总是在寻找最干净、最纯粹的方式.任何建议都非常感谢.
I know how to sort this out with jQuery - but always looking for cleanest, purest way. Any suggestion much appreciated.
皮特
推荐答案
使用 (CSS2) 属性选择器:
Using the (CSS2) attribute selector:
.error[for="username"]
这适用于 IE8+ 和所有现代浏览器.
This will work in IE8+ and all modern browsers.
IE7 的属性选择器有问题:如此处所述,要匹配 for
,您必须使用 htmlFor
.
IE7's attribute selector is buggy: as explained here, to match for
you must use htmlFor
.
所以,如果您需要 IE7 支持,请使用:
So, if you need IE7 support, use this:
.error[for="username"], .error[htmlFor="username"]
这篇关于CSS 选择器 - 如何在 CSS 中选择“for"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CSS 选择器 - 如何在 CSS 中选择“for"?


基础教程推荐
- 如何添加到目前为止的天数? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01