admin管理员组文章数量:1394759
I have multiple removeAttr()
is there a way I can bine them?
What I have: (this works)
checkboxes.removeAttr("checked",'checked');
checkboxes.removeAttr("indeterminate", false);
checkboxes.removeAttr("role", 'checkbox');
checkboxes.removeAttr("aria-checked", false);
What I have tried: (this doesn't work)
checkboxes.removeAttr({
checked: 'checked',
indeterminate: false,
role: 'checkbox',
'aria-checked': false
});
When I try this I get the following console error. So either my syntax is wrong or this just doesn't work.
Uncaught TypeError: Object #<Object> has no method 'toLowerCase' jquery.min.js:2
Any help is appreciated. Thanks.
I have multiple removeAttr()
is there a way I can bine them?
What I have: (this works)
checkboxes.removeAttr("checked",'checked');
checkboxes.removeAttr("indeterminate", false);
checkboxes.removeAttr("role", 'checkbox');
checkboxes.removeAttr("aria-checked", false);
What I have tried: (this doesn't work)
checkboxes.removeAttr({
checked: 'checked',
indeterminate: false,
role: 'checkbox',
'aria-checked': false
});
When I try this I get the following console error. So either my syntax is wrong or this just doesn't work.
Uncaught TypeError: Object #<Object> has no method 'toLowerCase' jquery.min.js:2
Any help is appreciated. Thanks.
Share Improve this question edited Feb 9, 2016 at 16:51 khollenbeck asked Feb 28, 2013 at 16:46 khollenbeckkhollenbeck 16.2k18 gold badges68 silver badges102 bronze badges 2-
2
.removeAttr()
only takes one parameter.An attribute to remove; as of version 1.7, it can be a space-separated list of attributes.
– gen_Eric Commented Feb 28, 2013 at 16:49 -
Aren't you really looking for
.prop()
? – j08691 Commented Feb 28, 2013 at 16:49
4 Answers
Reset to default 5Just use $.each
with an array, quick, short, and expandable.
$.each(["checked","indeterminate"],function(i,attrName){
checkbox.removeAttr(attrName);
});
According to removeAttr() doc it does not accept an object. so
checkboxes.removeAttr({});
will not work. and it also accept one paremeter [thx @RocketHazmat] for correction to which you want to remove.
You can do a chaining like:
checkboxes
.removeAttr("checked")
.removeAttr("indeterminate")
.removeAttr("role")
.removeAttr("aria-checked");
But if you want to set attribute then you've to use attr()
method like:
checkboxes
.attr("checked", 'checked')
.attr("indeterminate",false )
.attr("role",'checkbox')
.attr("aria-checked", false);
I'm not sure why you're passing both keys and values to removeAttr
, but if you just want to remove those 4 attributes from an element, pass a space-separated list of the keys, like so:
checkboxes.removeAttr("checked indeterminate role aria-checked");
@RocketHazmat is right, so it would be like this:
checkboxes.removeAttr("checked indeterminate role aria-checked");
But you need 1.7 or later from jQuery.
本文标签: javascriptCombing multiple removeAttr() statementsStack Overflow
版权声明:本文标题:javascript - Combing multiple .removeAttr() statements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744102163a2590915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论