[英]Adding a style using CSS works, but using JS to apply same style doesn't
[英]Changing style using JS & HTML works, JS & CSS doesn't always
使用JS和HTML标记始终有效: Codepen
if (document.forms['myForm'].name.value == '') {
name.style.background = "pink";
}
else {
name.style.background = "white";
}
使用JS和CSS 并不总是有效: Codepen
if (document.forms['myForm'].name.value == '') {
// give this element the .error class from the css file
name.className = name.className + " error";
}
else {
// this removes the error class
name.className = name.className.replace(" error", "");
}
为什么JS和CSS不一致? 我想使用这种方法,因为我可以将错误类应用于我想要影响的每个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.