1

我有多个removeAttr()有没有办法可以组合它们?

我所拥有的:(这行得通)

checkboxes.removeAttr("checked",'checked');
checkboxes.removeAttr("indeterminate", false);
checkboxes.removeAttr("role", 'checkbox');
checkboxes.removeAttr("aria-checked", false);

我尝试过的:(这不起作用)

checkboxes.removeAttr({
    checked: 'checked',
    indeterminate: false,
    role: 'checkbox',
    'aria-checked': false
});

当我尝试这个时,我得到以下控制台错误。所以要么我的语法错误,要么这不起作用。

Uncaught TypeError: Object #<Object> has no method 'toLowerCase'  jquery.min.js:2

任何帮助表示赞赏。谢谢。

4

4 回答 4

5

只需$.each与阵列一起使用,快速、简短且可扩展。

$.each(["checked","indeterminate"],function(i,attrName){
    checkbox.removeAttr(attrName);
});
于 2013-02-28T16:50:37.943 回答
3

根据removeAttr()文档,它不接受对象。所以

checkboxes.removeAttr({});不管用。它还接受一个参数 [thx @RocketHazmat] 进行更正,您想删除它。

您可以进行如下链接:

checkboxes
   .removeAttr("checked")
   .removeAttr("indeterminate")
   .removeAttr("role")
   .removeAttr("aria-checked");

但是如果你想设置属性,那么你必须使用attr()如下方法:

checkboxes
  .attr("checked", 'checked')
  .attr("indeterminate",false )
  .attr("role",'checkbox')
  .attr("aria-checked", false);
于 2013-02-28T16:47:52.767 回答
1

我不确定您为什么将键和值都传递给removeAttr,但如果您只想从元素中删除这 4 个属性,请传递以空格分隔的键列表,如下所示:

checkboxes.removeAttr("checked indeterminate role aria-checked");
于 2013-02-28T16:52:08.813 回答
1

@RocketHazmat 是对的,所以它会是这样的:

checkboxes.removeAttr("checked indeterminate role aria-checked");

但是你需要 jQuery 的 1.7 或更高版本。

于 2013-02-28T16:52:16.803 回答