代码之家  ›  专栏  ›  技术社区  ›  David Hellsing

删除所有属性

  •  31
  • David Hellsing  · 技术社区  · 16 年前

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

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

    <img>
    

    我试过 $('img').removeAttr('*'); 运气不好。有人吗?

    8 回复  |  直到 7 年前
        1
  •  44
  •   cletus    16 年前

    更新: 以前的方法在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();
    
        2
  •  53
  •   user1846065    10 年前

    不需要jquery的简单方法:

    while(elem.attributes.length > 0)
        elem.removeAttribute(elem.attributes[0].name);
    
        3
  •  4
  •   Nik Sumeiko    11 年前

    而不是创建新的 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);
    };
    

    现在你可以打电话了 ReleWrad() 没有参数从元素中移除所有属性:

    $('img').removeAttr();
    
        4
  •  3
  •   user993683    7 年前

    一行程序,无需jquery:

    [...elem.attributes].forEach(attr => elem.removeAttribute(attr.name));
    
        5
  •  1
  •   Panoone    14 年前

    为特定的标签执行此操作的一个非常好的原因是清理遗留内容,并强制执行标准。

    比如说,您希望删除遗留属性,或者通过剥离字体标记属性来限制它们造成的损坏。

    我已经尝试了几种方法来实现这一点,但是没有一种方法,包括上面的例子,可以按需要工作。

    示例1:用包含的文本内容替换所有字体标记。 这将是一个完美的解决方案,但是从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,它承诺包含一个按名称删除多个属性的方法。

        6
  •  0
  •   sjobe    16 年前

    我不知道你用它来做什么,但是你考虑过用CSS类来代替它并切换它们吗?它会减少你的编码,减少浏览器的工作。如果您正在动态生成一些属性(如with和height),这可能不太容易。

        7
  •  0
  •   Ahmet Can Güven    13 年前

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

        var x = document.createElement($("#some_id").prop("tagName"));
        $(x).insertAfter($("#some_id"));
        $("#some_id").remove();
    
        8
  •  0
  •   Стас Пишевский    8 年前

    今天我也有同样的问题。我想对你有用

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