如何通过属性名称删除所有属性始于 [英] How to remove all Attributes by attribute name starts with

查看:97
本文介绍了如何通过属性名称删除所有属性始于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在具有只读状态"类的字段中删除所有属性名称以"data-val"开头的属性.

I would like to remove all the attributes that attribute name starts with 'data-val' in the fields that has a class 'read-only-state'

 jQuery("[data-val^='tr']" )  

这将给出以'tr'开头的'data-val'属性值

This will give attribute 'data-val' value that starts with 'tr'

但是我需要删除所有以 匹配元素中的"data-val" .

But i need to remove all the attributes that starts with 'data-val' in the matched elements.

我该怎么办?

推荐答案

您可以为此使用普通javascript的attributes:

You can use vanilla javascript's attributes for this:

$('.read-only-state').each(function() {
   // get the native attributes object
   var attrs = this.attributes;
   var toRemove = [];
   // cache the jquery object containing the element for better performance
   var element = $(this);

   // iterate the attributes
   for (attr in attrs) {
     if (typeof attrs[attr] === 'object' && 
         typeof attrs[attr].name === 'string' && 
         (/^data-val/).test(attrs[attr].name)) {
       // Unfortunately, we can not call removeAttr directly in here, since it
       // hurts the iteration.
       toRemove.push(attrs[attr].name);
     }
   }

   for (var i = 0; i < toRemove.length; i++) {
     element.removeAttr(toRemove[i]);
   }
});

这篇关于如何通过属性名称删除所有属性始于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆