【发布时间】:2018-09-20 23:09:27
【问题描述】:
嘿嘿。我有个问题。如果输入的值为空,我想创建标签删除类,否则(如果输入有值)
HTML
<div class="input-field">
<input type="email" id="username" name="email" required>
<label for="username">Email Address</label>
</div>
<div class="input-field">
<input type="password" id="password" name="password" required>
<label for="password">Password</label>
</div>
jQuery
$(document).ready(function(){
if ($(".input-field:first-of-type input").val() == '') {
$(".input-field:first-of-type label").removeClass("active");
}
else {
$(".input-field:first-of-type label").addClass("active");
}
});
CSS
label.active {
color: red;
}
我希望,<div class="input-field"> 内部的label 具有active 的类时将是红色的。但它不起作用。请问有什么办法吗?
【问题讨论】:
-
您好,我刚刚在 jsfiddle 上更新了您的代码,现在它可以按照您的要求工作。希望对你有帮助
标签: jquery