Is there a way to clone elements using JQuery?

前端 未结 3 777
予麋鹿
予麋鹿 2020-12-21 08:24

Is there a way to clone HTML elements using JQuery?

However, I want to be able to assign the cloned elements new attributes \"id, name\". So if I had a textfield ele

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 09:10

    documentation

    I use jQuery over $ so it works with other libraries. Here is an example on an input text field.

    jQuery('#example').clone().val('').attr('id', 'exmple2').attr('name', 'exmple2').insertAfter('#example');
    

    If you want to clone the event handlers as well, you have to use clone(true).

提交回复
热议问题