使用jQuery的removeAttr删除多个属性

som*_*osh 85 jquery

我有以下代码.

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

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

Den*_*ret 162

是的:

.removeAttr('height align style')
Run Code Online (Sandbox Code Playgroud)

文档:

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


Pri*_*Ray 8

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

$('#listing img').removeAttr('height align style');
Run Code Online (Sandbox Code Playgroud)

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

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