开发者

jQuery: How to override pseudo style?

开发者 https://www.devze.com 2023-01-13 01:36 出处:网络
So here\'s the summary CSS: input[type=\"text\"]:focus{background:blue;} .error{background:red;} Javascript:

So here's the summary

CSS:

input[type="text"]:focus{background:blue;}
.error{background:red;}

Javascript:

if(error){ $('#elementid').focus().addClass('error');

Problem:

The field goes red then goes blue immediately and stays blue when it is in开发者_JS百科 focus.

How do I make the 'error' class take precedence, when the javascript executes and the focus goes to the 'elementid' field and not the :focus class.


You can give .error more specificity, like this:

input[type="text"].error{background:red;}​
//or if shared:
.error, input[type="text"].error{background:red;}​

You can give it a try here, with the same level of specificity, error will win because it's declared later in the CSS.

0

精彩评论

暂无评论...
验证码 换一张
取 消