jQuery clone duplicate IDs

后端 未结 9 1780
醉梦人生
醉梦人生 2020-12-04 19:03

I have an HTML element with a large collection of unordered lists contained within it. I need to clone this element to place elsewhere on the page with different styles adde

相关标签:
9条回答
  • 2020-12-04 19:50

    Since the OP asked for a way to replace all the duplicate id's before appending them, maybe something like this would work. Assuming you wanted to clone MainConfig_1 in an HTML block such as this:

    <div id="smallConfig">
        <div id="MainConfig_1">
            <ul>
                <li id="red_1">red</li>
                <li id="blue_1">blue</li>
            </ul>
        </div>
    </div>
    

    The code could be something like the following, to find all child elements (and descendants) of the cloned block, and modify their id's using a counter:

    var cur_num = 1;    // Counter used previously.
    //...
    var cloned = $("#MainConfig_" + cur_num).clone(true, true).get(0);
    ++cur_num;
    cloned.id = "MainConfig_" + cur_num;                  // Change the div itself.
    $(cloned).find("*").each(function(index, element) {   // And all inner elements.
        if(element.id)
        {
            var matches = element.id.match(/(.+)_\d+/);
            if(matches && matches.length >= 2)            // Captures start at [1].
                element.id = matches[1] + "_" + cur_num;
        }
    });
    $(cloned).appendTo($("#smallConfig"));
    

    To create new HTML like this:

    <div id="smallConfig">
        <div id="MainConfig_1">
            <ul>
                <li id="red_1">red</li>
                <li id="blue_1">blue</li>
            </ul>
        </div>
        <div id="MainConfig_2">
            <ul>
                <li id="red_2">red</li>
                <li id="blue_2">blue</li>
            </ul>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-04 19:50
    $("#MainConfig")
        .clone(false)
        .find("ul,li")
        .removeAttr("id")
        .appendTo($("#smallConfig"));
    

    Try that on for size. :)

    [Edit] Fixed for redsquare's comment.

    0 讨论(0)
  • 2020-12-04 19:51

    This is based on Russell's answer but a bit more aesthetic and functional for forms. jQuery:

    $(document).ready(function(){
       var cur_num = 1;    // Counter
    
        $('#btnClone').click(function(){
    
              var whatToClone = $("#MainConfig"); 
              var whereToPutIt = $("#smallConfig");
    
              var cloned = whatToClone.clone(true, true).get(0);
              ++cur_num;
              cloned.id = whatToClone.attr('id') + "_" + cur_num;                  // Change the div itself.
    
            $(cloned).find("*").each(function(index, element) {   // And all inner elements.
              if(element.id)
              {
                  var matches = element.id.match(/(.+)_\d+/);
                  if(matches && matches.length >= 2)            // Captures start at [1].
                      element.id = matches[1] + "_" + cur_num;
              }
              if(element.name)
              {
                  var matches = element.name.match(/(.+)_\d+/);
                  if(matches && matches.length >= 2)            // Captures start at [1].
                      element.name = matches[1] + "_" + cur_num;
              }
    
             });
    
           $(cloned).appendTo( whereToPutIt );
    
        });
    });
    

    The Markup:

    <div id="smallConfig">
        <div id="MainConfig">
            <ul>
                <li id="red_1">red</li>
                <li id="blue_1">blue</li>
            </ul>
          <input id="purple" type="text" value="I'm a text box" name="textboxIsaid_1" />
        </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题