$('#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');

09-20 13:15