2009-12-08 117 views
27

是否有可能使用jQuery一次刪除所有屬性?刪除所有屬性

<img src="example.jpg" width="100" height="100"> 

<img> 

我試圖$('img').removeAttr('*');沒有運氣。任何人?

+0

壞主意,你應該只刪除您知道,一個DOM元素具有比您在標籤中指定每一個都有自己默認的是什麼了更多的屬性什麼屬性或計算值。 – row1 2009-12-09 03:03:29

回答

40

更新:上述方法適用於IE8,但不適用於IE8兼容模式和以前版本的IE。所以在這裏是不和使用jQuery的因爲它的一個更好的工作,除去屬性的版本:

$("img").each(function() { 
    // first copy the attributes to remove 
    // if we don't do this it causes problems 
    // iterating over the array we're removing 
    // elements from 
    var attributes = $.map(this.attributes, function(item) { 
    return item.name; 
    }); 

    // now use jQuery to remove the attributes 
    var img = $(this); 
    $.each(attributes, function(i, item) { 
    img.removeAttr(item); 
    }); 
}); 

你當然可以make a plug-in出來的:

jQuery.fn.removeAttributes = function() { 
    return this.each(function() { 
    var attributes = $.map(this.attributes, function(item) { 
     return item.name; 
    }); 
    var img = $(this); 
    $.each(attributes, function(i, item) { 
    img.removeAttr(item); 
    }); 
    }); 
} 

然後執行:

$("img").removeAttributes(); 
+0

不需要插件,這是我以後的while循環。謝謝! – David 2009-12-08 23:19:27

+0

錯誤,沒有。這對於IE來說並不是一個可行的解決方案。 'removeAttribute'完全不起作用,就像你期望的那樣(提示:'while循環將永遠鎖定可憐的瀏覽器')。 – 2009-12-09 02:37:49

+1

剛剛遇到了這個問題 - 我希望你可以一次性在所有計算機上永遠鎖定IE,並且只允許一個標籤工作,它指向Chrome下載頁面。那將是真棒! (並且不,我不是說允許IE 9或者10 - IE 6被/ **仍然是**那麼垃圾,IE 9和10應該受到影響!) – ClarkeyBoy 2013-02-11 19:03:24

0

我不確切知道你在用什麼,但你有沒有考慮過使用CSS類而不是切換這些?它會減少編碼,減少瀏覽器的工作量。如果你像使用和height一樣快速生成一些屬性,這可能無法[容易]運行。

1

爲特定標籤做這件事的一個很好的理由是清理遺留內容並執行標準。例如,您想刪除舊版屬性,或者通過刪除FONT標籤屬性來限制造成的損壞。

我已經嘗試了幾種方法來實現這一點,但沒有一個包括上面的例子,按照需要工作。

示例1:將所有FONT標籤替換爲包含的文本內容。 這將是一個完美的解決方案,但v1.6.2版已停止運作。 :(

$('#content font').each(function(i) { 
    $(this).replaceWith($(this).text()); 
}); 

例2:剝去了一個名爲標籤的所有屬性 - 例如,字體 同樣,這無法正常使用,但我相信它曾經在以前版本的jQuery一旦工作

$("font").each(function() { 
// First copy the attributes to remove. 
var attributes = $.map(this.attributes, function(item) { 
    return item.name; 
});  
// Now remove the attributes 
var font = $(this); 
$.each(attributes, function(i, item) { 
    $("font").removeAttr(item); 
}); 
}); 

期待1.7它承諾包括一個方法來刪除多個屬性的名稱

0

這將刪除所有屬性,它將適用於每種類型的元素。

var x = document.createElement($("#some_id").prop("tagName")); 
    $(x).insertAfter($("#some_id")); 
    $("#some_id").remove(); 
+2

這也刪除事件綁定。 – 2014-03-15 05:21:54

+0

那麼下降投票的原因是什麼? – 2014-03-15 10:21:04

36

不需要JQuery的一個簡單的方法:

while(elem.attributes.length > 0) 
    elem.removeAttribute(elem.attributes[0].name); 
+1

最好的代碼是最簡單的代碼。沒有jQuery,沒有局部變量,只是一個循環。謝謝! – StanE 2016-02-29 13:10:54

+1

如果選擇了JQuery對象,請小心。在使用此方法之前,您需要將其轉換爲htmlElement([$ elem.get(0)](https://api.jquery.com/get/))。 – 2016-07-14 14:21:40

3

不是創建一個新jQuery.fn.removeAttributes(在接受的答案證明),可以延長jQuery的現有.removeAttr()方法使得它接受零個參數請從每個元素的所有屬性的集合:

var removeAttr = jQuery.fn.removeAttr; 
jQuery.fn.removeAttr = function() { 

    if (!arguments.length) { 
    this.each(function() { 

     // Looping attributes array in reverse direction 
     // to avoid skipping items due to the changing length 
     // when removing them on every iteration. 
     for (var i = this.attributes.length -1; i >= 0 ; i--) { 
     jQuery(this).removeAttr(this.attributes[i].name); 
     } 
    }); 

    return this; 
    } 

    return removeAttr.apply(this, arguments); 
}; 

現在,您可以撥打.removeAttr()不帶參數刪除所有ATT從元素肋骨:

$('img').removeAttr(); 
0

今天我有同樣的問題。我認爲,這將是對您有用

var clone = $(node).html(); 
clone = $('<tr>'+ clone +'</tr>'); 
clone.addClass('tmcRow');