开发者

How to remove attributes at a time using jquery

$('#myImage').removeAttr('class').removeAttr('style').removeAttr('border');

That one works开发者_Python百科 just fine but is there a way to remove a set of attributes from an element?

I altered the code a bit, like

$.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;
}

call it like below

$('#myImage').removeAttrs('class style border');


No, but you could write your own easily enough:

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

Here's one with a few different overloads, jQuery style:

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


You already have a function in jquery

jQuery(element).removeAttr('attribute1 attribuite2 attribute3');

will remove all attributes in one go .

http://api.jquery.com/removeAttr/ here removeAttr can consume a list

Demo : just even for this very stackoverflow page in developer console do

jQuery('div#footer').removeAttr('id class')

This will remove both attributes id and class from the div


I wrote this, SO HAPPY!

// classNames is an array of attribute names you want to remove from the element.
// jQueryElement is the jQuery element to remove the attributes from.
function removeSetOfAttributesFromAnElement(attNames, jQueryElement) {
    $.each(attNames, function() {
        jQueryElement.removeAttr(this);
    }
}

// call it!
removeSetOfAttributesFromAnElement(['class','style','border'], $("#myImage"));


Not directly. First ask is that really what you want to do. Bearing in mind you will loose the Id attribute.

But if you must what you could do is really create a new tag same as your source tag (DIV, etc) then get the source html using $('#target').html($('#source').html());

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜