内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
我试图通过单击不同的单选按钮来添加和删除文本字段中的多个类。在切换不同的单选按钮时,我无法删除不需要的类。我的代码是:
//For 1st radio button if(actionUrl == "search-client-by-id") { $("#req").removeClass("validate[required,custom[onlyLetterNumber],maxSize[20],custom[onlyLetterSp]]").addClass("validate[required,custom[onlyNumberSp]]"); } //For 2nd radio button else if(actionUrl == "search-client-by-name") { $("#req").removeClass("validate[required,custom[onlyNumberSp]]").addClass("validate[required,custom[onlyLetterNumber],maxSize[20],custom[onlyLetterSp]]"); }
使用javascript追加类名最简单的方法。.siblings()
行为不当时可能有用。
document.getElementById('myId').className += ' active';