0

クラス「read-only-state」を持つフィールドで、属性名が「data-val」で始まるすべての属性を削除したい

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

これにより、「tr」で始まる属性「data-val」の値が得られます。

ただし、一致した要素の「data-val」で始まるすべての属性を削除する必要があります。

どうすればいいですか?

4

1 に答える 1

7

これには、バニラの JavaScript を使用できますattributes

$('.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]);
   }
});
于 2013-02-06T14:36:37.970 に答える