How can I change HTML attribute names with jQuery?

后端 未结 7 1415
無奈伤痛
無奈伤痛 2020-11-30 06:51

I would like to change all the names of the attributes where class=\"testingCase\" throughout all my whole html document.

e.g. Change:

&         


        
7条回答
  •  暖寄归人
    2020-11-30 07:07

    There is no built-in method/function to "rename" an attribute in javascript, but you can create new attributes and remove other ones...

    $('a.testingCase[title]').each(function() {
      var $t = $(this);
      $t.attr({
          newTitleName: $t.attr('title')
        })
        .removeAttr('title');
    });
    
    Blabla
    Bloo

    Edit: added in the bit that makes it only select a elements with class="testingCase"

提交回复
热议问题