使用jQuery的removeAttr删除多个属性


93

我有以下代码。

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

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

Answers:


176

是的:

.removeAttr('height align style')

文档中

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


1
天哪,这是一个快速的答复。非常感谢你。我知道这是removeStyle的选项,但是我在removeAttr上找不到任何东西。感谢分享。
somecallmejosh 2012年

如果不是jquery 1.7+,该怎么办?
Patoshiパトシ2014年

@duckx更新。使用旧版本的jQuery没有任何意义。已修复了许多错误,并不断开发jQuery以跟上浏览器的发展。
DenysSéguret2014年

3

是的,您可以通过以下方式将其删除:

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

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

$('#listing img').attr({ height: "20", align: left }).css({ color: red, text-align: center });
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.