$('#myImage').removeAttr('class').removeAttr('style').removeAttr('border');
这个工作得很好,但是有没有办法从元素中删除一组属性?
我稍微修改了代码,例如
$.fn.removeAttrs = function () {
// Convert all passed arguments to an array
var args = arguments[0].split(' '),
attr;
// Loop, removing the first array item on each iteration
while (attr = args.shift())
this.removeAttr(attr);
// Return the jQuery object for chaining
return this;
}
像下面这样调用
$('#myImage').removeAttrs('class style border');
最佳答案
不,但你可以很容易地编写自己的:
$.fn.removeAttrs = function () {
// Convert all passed arguments to an array
var args = Array.prototype.slice.call(arguments),
attr;
// Loop, removing the first array item on each iteration
while (attr = args.shift())
this.removeAttr(attr);
// Return the jQuery object for chaining
return this;
}
$('#myImage').removeAttrs('class', 'style', 'border');
这里有一些不同的重载,jQuery 风格:
$.fn.removeAttrs = function () {
// Convert all passed arguments to an array
var args = Array.prototype.slice.call(arguments),
attr;
// Handle passing arrays or space-separated strings
if (args.length == 1)
args = $.isArray(args[0]) ? args[0] : args[0].split(" ");
// Loop, removing the first array item on each iteration
while (attr = args.shift())
this.removeAttr(attr);
// Return the jQuery object for chaining
return this;
}
// Now all of the following will work
$('#myImage').removeAttrs(['class', 'style', 'border']);
$('#myImage').removeAttrs('class', 'style', 'border');
$('#myImage').removeAttrs('class style border');
关于jquery - 如何使用jquery一次删除属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4183051/