JQuery adding class to cloned element

后端 未结 2 1122
攒了一身酷
攒了一身酷 2021-02-20 04:11

This is my script:

$(\'.addprop\').click(function() {
        $(\'#clone\').clone().insertAfter(\'.addprop\');
    })

I need to add a class to

2条回答
  •  时光取名叫无心
    2021-02-20 04:42

    Sure.

    After the .clone() method the current element is the clone..

    $('#clone').clone().addClass('class-name-here').insertAfter('.addprop');
    

    Notice

    you will need to change the id of the clone as it must be unique in the DOM and when you clone that element, the id is cloned as well..

    So better to do something like

    $('#clone').clone().attr('id','newid').addClass('class-name-here').insertAfter('.addprop');
    

提交回复
热议问题