問題描述
我正在使用 jQuery 驗(yàn)證.
I'm using jQuery validation.
當(dāng) for 現(xiàn)在有效時(shí),驗(yàn)證器正在創(chuàng)建這些:
When for is now valid, validator is creating those:
<label class="error" for="username" generated="true">
由于所有標(biāo)簽都具有相同的 class=error,我可以根據(jù) 'for' 使用 CSS 選擇這個(gè)確切的標(biāo)簽嗎?
As all label has got the same class=error can I select with CSS this exact one based on 'for'?
我知道如何用 jQuery 來解決這個(gè)問題——但總是在尋找最干凈、最純粹的方式.任何建議都非常感謝.
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+ 和所有現(xiàn)代瀏覽器.
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"]
這篇關(guān)于CSS 選擇器 - 如何在 CSS 中選擇“for"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!