How can I change HTML attribute names with jQuery?

后端 未结 7 1381
無奈伤痛
無奈伤痛 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:02

    One liner

    $('selector').replaceWith($('selector')[0].outerHTML.replace("oldName=","newName="));
    

    worked great for me when I needed to strip a prefix from all of my attributes

    $('selector').replaceWith($('selector')[0].outerHTML.(/prefix\-/g,""));
    
    0 讨论(0)
  • 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');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a class="testingCase" href="#" title="name of testing case">Blabla</a>
    <a class="testingCase" href="#" title="name of another testing case">Bloo</a>

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

    0 讨论(0)
  • 2020-11-30 07:07

    same answer as @nickf but cleaner code:

    $('a.testingCase[title]').each(function() {
        var curElem = $(this);
        curElem.attr("newTitleName", curElem.attr('title')).removeAttr('title');
    });
    
    0 讨论(0)
  • 2020-11-30 07:09

    It works for me!

    $('input[name="descricao"]').attr('name', 'title');
    
    0 讨论(0)
  • 2020-11-30 07:22

    I don't think you can change an attribute name but what you can do is :

    • get all the <a> tags with a title attribute;
    • foreach of them, create a newTitleName attribute with the same value as the title;
    • then delete the title attribute.

    JQuery let you do that with builtin functions this way :

    /* get all <a> with a "title" attribute that are testingCase
    then apply an anonymous function on each of them */
    
    $('a.testingCase[title]').each(function() {   
    
        /* create a jquery object from the <a> DOM object */
        var $a_with_title = $(this);    
    
        /* add the new attribute with the title value */
        $a_with_title.attr("newTitleName", $a_with_title.getAttribute('title'));
    
        /* remove the old attribute */
        $a_with_title.removeAttr('title'); 
    
    });
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题