删除所有属性 [英] Remove all attributes
本文介绍了删除所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用jQuery一次删除所有属性?
Is it possible to remove all attributes at once using jQuery?
<img src="example.jpg" width="100" height="100">
到
<img>
我试过 $('img')。removeAttr('*') ;
没有运气。任何人?
I tried $('img').removeAttr('*');
with no luck. Anyone?
推荐答案
更新:以前的方法适用于IE8但不适用于IE8兼容模式和之前的版本IE的版本。所以这里有一个版本,它使用jQuery删除属性,因为它可以更好地完成它:
Update: the previous method works in IE8 but not in IE8 compatibility mode and previous versions of IE. So here is a version that does and uses jQuery to remove the attributes as it does a better job of it:
$("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);
});
});
当然你可以制作一个插件:
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();
这篇关于删除所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文