问题: 1.如何按类名引用父元素?一旦我引用它,如何在不删除当前类名的情况下添加新的类名?
以下是我当前HTML的结构:
<div class="form-group">
<div class="col-md-12">
<div class="row">
<div class="col-md-4 col-xs-12 no-pad">
<input type="text" class="form-control" id="employeeCount" name="employeeCount" placeholder="{{nls 'example_total_num'}}" value="{{employeeCount}}"></input>
<i class="material-icons">error_outline</i>
<div class="help-block"> </div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-12 no-pad">
<input type="text" class="form-control" id="salesForceCount" name="salesForceCount" placeholder="{{nls 'example_sales_num'}}" value="{{salesForceCount}}"></input>
<i class="material-icons">error_outline</i>
<div class="help-block"> </div>
</div>
</div>
</div>
</div>
以下是与上面#salesForceCount
输入的模糊事件相关联的方法:
procSalesForceCount: function (e) {
let $e = $(e.currentTarget);
let val = parseInt($e.val());
val = isNaN(val) ? null: val;
if (val > this.model.get('employeeCount')) {
var error_message = `${val} is larger than employee count ${this.model.get("employeeCount")}`;
//add new class has-error to parent div with form-group class
$e.closest(".form-group").addClass("has-error");
$e.siblings('.help-block').html(error_message);
return;
}
// END
this.model.set({ salesForceCount: val });
}
答案 0 :(得分:2)
- 如何按类名引用父元素?一旦我引用它,如何在不删除当前类名的情况下添加新的类名?
醇>
$(child).closest(".class-name").addClass("new-class");
- 我如何按类名引用兄弟元素?
醇>
$(selector).siblings(".class-name");
或者
$("selector + .class-name");