How can I change HTML attribute names with jQuery?

后端 未结 7 1412
無奈伤痛
無奈伤痛 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条回答
  •  Happy的楠姐
    2020-11-30 07:22

    Here's a simple jquery-style plugin that gives you a renameAttr method:

    // Rename an entity attribute
    jQuery.fn.renameAttr = function(oldName, newName) {
        var args = arguments[0] || {}; 
        var o = $(this[0]) 
    
           o
            .attr(
                newName, o.attr(oldName)
            )
            .removeAttr(oldName)
            ;
    };
    

    There is also this example which adds an option to remove the data for the old attribute.

提交回复
热议问题