CSS 选择器 - 如何在 CSS 中选择“for"? [英] CSS selectors - how to select 'for' in CSS?

查看:227
本文介绍了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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆