[英]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.