Javascript 删除所有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1870441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:41:37  来源:igfitidea点击:

Remove all attributes

javascriptjquery

提问by David Hellsing

Is it possible to remove all attributes at once using jQuery?

是否可以使用 jQuery 一次删除所有属性?

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

to

<img>

I tried $('img').removeAttr('*');with no luck. Anyone?

我试过$('img').removeAttr('*');没有运气。任何人?

回答by

A simple method that doesn't require JQuery:

一个不需要 JQuery 的简单方法:

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

回答by cletus

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:

更新:之前的方法适用于 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);
  });
});

Of course you could make a plug-inout of it:

当然你可以用它做一个插件

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);
    });
  });
}

and then do:

然后做:

$("img").removeAttributes();

回答by David Hellsing

One-liner, no jQuery needed:

单行,不需要 jQuery:

[...elem.attributes].forEach(attr => elem.removeAttribute(attr.name));

回答by Nik Sumeiko

Instead of creating a new jQuery.fn.removeAttributes(demonstrated in the accepted answer) you can extend jQuery's existing .removeAttr()method making it accept zero parameters to remove all attributes from each element in a set:

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);
};

Now you can call .removeAttr()without parameters to remove all attributes from the element:

现在您可以.removeAttr()不带参数调用以从元素中删除所有属性:

$('img').removeAttr();

回答by Panoone

One very good reason to do this for specific tags is to clean up legacy content and also enforce standards.

对特定标签执行此操作的一个很好的理由是清理遗留内容并强制执行标准。

Let's say, for example, you wanted to remove legacy attributes, or limit damage caused by FONT tag attributes by stripping them.

比方说,例如,您想要删除遗留属性,或者通过剥离它们来限制由 FONT 标签属性造成的损坏。

I've tried several methods to achieve this and none, including the example above, work as desired.

我尝试了几种方法来实现这一点,但没有一种方法(包括上面的示例)可以按预期工作。

Example 1: Replace all FONT tags with the contained textual content. This would be the perfect solution but as of v1.6.2 has ceased to function. :(

示例 1:将所有 FONT 标记替换为包含的文本内容。这将是完美的解决方案,但自 v1.6.2 起已停止运行。:(

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

Example 2: Strip all attributes from a named tag - e.g. FONT. Again, this fails to function but am sure it used to work once upon a previous jQuery version.

示例 2:从命名标签中去除所有属性 - 例如 FONT。同样,这无法正常工作,但我确信它曾经在以前的 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);
 });
});

Looking forward to 1.7 which promises to include a method to remove multiple attributes by name.

期待 1.7,它承诺包含一种按名称删除多个属性的方法。

回答by Ahmet Can Güven

This will remove all attributes and it will work for every type of element.

这将删除所有属性,它将适用于每种类型的元素。

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

回答by sjobe

I don't know exactly what you're using it for, but have you considered using css classes instead and toggling those ? It'll be less coding on your side and less work for the browser to do. This will probably not work [easily] if you're generating some of the attributes on the fly like with and height.

我不确切知道您使用它的目的,但是您是否考虑过使用 css 类并切换它们?您这边的编码会减少,浏览器要做的工作也会减少。如果您正在动态生成一些属性,例如 with 和 height,这可能 [很容易] 不起作用。

回答by Стас Пишевский

Today I have same issue. I think that it will be useful for you

今天我有同样的问题。我认为这对你有用

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