How to clone() a element n times?

前端 未结 2 1161
渐次进展
渐次进展 2021-01-04 21:10

I have a dynamic table that I want to attach elements with jQuery.

I have this:

var tds = jQuery(\"tr > td\").length; //          


        
2条回答
  •  情话喂你
    2021-01-04 21:34

    If you don't want deep clones, then you can avoid the manual iteration by passing the outerHTML of the element to an arrays join() method resulting in an HTMLString corresponding to n number of elements as shown below:

    var elementString = new Array(++n).join($(selector).get(0).outerHTML)
    

    which you can append to any element you wish.


    In your case you can do:

    var n= $("tr > td").length,
    $colgroup = $("#colgroup-compare");
    $colgroup.append(new Array(++n).join($colgroup.find("col").get(0).outerHTML));
    

提交回复
热议问题