您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

是否可以在JavaScript / JQuery中克隆html元素对象?

是否可以在JavaScript / JQuery中克隆html元素对象?

使用您的代码,您可以使用cloneNode()方法在纯JavaScript中执行以下操作:

// Create a clone of element with id ddl_1:
let clone = document.querySelector('#ddl_1').cloneNode( true );

// Change the id attribute of the newly created element:
clone.setAttribute( 'id', newId );

// Append the newly created element on element p 
document.querySelector('p').appendChild( clone );

或使用jQuery clone()方法(不是最有效的):

$('#ddl_1').clone().attr('id', newId).appendTo('p'); // append to where you want
javascript 2022/1/1 18:20:48 有316人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶