简体   繁体   English

带有:after选择器的Javascript addClass

[英]Javascript addClass with :after selector

I am using addClass to validate my form. 我正在使用addClass来验证我的表单。 This is my javascript:- 这是我的javascript:-

if (name.val()=='') {
    name.addClass('highlight');
    return false;
} else name.removeClass('highlight');

and the css with the class I am adding is this :- 和我要添加的类的CSS是:-

#coverup .highlight {
    border: 1px solid #a94442;
}

And this script above both works and is okay, however I want to add a glyphicon to the textfield as well as change the border colour, so that the error becomes more prominent to the user. 而且上面的脚本都可以正常工作,但是我想在文本字段中添加glyphicon并更改边框颜色,以使错误对用户更加突出。

I then decided to simply change the code by doing another addClass method as shown below:- 然后,我决定通过执行另一个如下所示的addClass方法来简单地更改代码:

if (name.val()=='') {
    name.addClass('highlight');
    name.addClass('highlight:after');
    return false;
} else name.removeClass('highlight'); name.removeClass('highlight:after');

and Then changed my CSS to this :- 然后将我的CSS更改为:

#coverup .highlight {
    border: 1px solid #a94442;
}
#coverup .highlight:after {
    content:"\2715";
    color: red;
}

However doing this hasn't solved my issue, can anybody give me some options or guidance as to how I can simply add a glyphicon to the end of my text field if it isn't filled in as well as making the border change colour too. 但是这样做并不能解决我的问题,任何人都可以给我一些选择或指导,以指示我如何在不填写文本字段的情况下向其文本字段的末尾添加一个glyphicon,以及如何使边框也更改颜色。

Thanks! 谢谢!

  • <input /> elements cannot have child nodes. <input />元素不能有子节点。
  • ::before and ::after psuedo-elements are treated as a child inserted before the first, and after the last "real" child respectively. ::before::after伪元素被分别视为在第一个“真实”孩子之前和之后插入的孩子。

The above two facts considered, you cannot apply ::before or ::after to <input /> elements. 考虑到以上两个事实, 您不能将::before::after应用于<input />元素。

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

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