使用 jQuery 的 removeAttr 删除多个属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13725301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:54:21  来源:igfitidea点击:

Remove multiple attributes with jQuery's removeAttr

jquery

提问by somecallmejosh

I have the following code.

我有以下代码。

$(document).ready(function(){
 $('#listing img')
 .attr('width', 250)
 .removeAttr('height').removeAttr('align').removeAttr('style')
 .wrap('<p />');
});

Is there a more efficient way of removing multiple attributes?

有没有更有效的方法来删除多个属性?

回答by Denys Séguret

Yes :

是的 :

.removeAttr('height align style')

From the documentation:

文档

as of version 1.7, it can be a space-separated list of attributes.

从 1.7 版开始,它可以是一个以空格分隔的属性列表。

回答by Pritam Jyoti Ray

Yes, you can remove it in that way:

是的,您可以通过这种方式删除它:

$('#listing img').removeAttr('height align style');

you can also add those attributes as follows:

您还可以按如下方式添加这些属性:

$('#listing img').attr({ height: "20", align: left }).css({ color: red, text-align: center });