从所有匹配的表单元素中删除一个类...最干净的方法?

gog*_*net 1 javascript methods performance jquery chaining

每当检查顶级单选按钮时,我都试图清除表单元素上的一堆错误样式.我试图找到:

  • 用于改善性能的替代方法和结构
  • 用于查找所有表单元素的链接方法,因此我没有进行如此多的调用..(不确定是否可能).

    //在调用下面的函数之前定义的全局变量OR范围变量.

JS

var target = jQuery("#cachedElement");


function functionWithManyReferencesToTarget() {
    target.find("input").each(function() {
    $(this).removeClass("formError error-state");
});
target.find("label").each(function() {
    $(this).removeClass("formError error-state");
});
target.find("select").each(function() {
    $(this).removeClass("formError error-state");
});
}
Run Code Online (Sandbox Code Playgroud)

Kyl*_*yle 6

$target.find('input, label, select').removeClass('formError error-state');
Run Code Online (Sandbox Code Playgroud)