如何将所有数据属性复制到元素

How can I copy all data-attrs to element?

本文关键字:复制 元素 数据属性      更新时间:2023-09-26

美好的一天,我有一些带有一些数据属性的HTML对象。但是我想将所有属性复制到另一个项目。例如:

<div data-attr-one="here is the data">content</div> <!-- This is an original item -->
<ul class="copied"></ul> <!-- This is a new item without data-attrs -->

我需要在 ul 项目中有一个数据属性

试试这个,

 var $div = $("div");
 var $ul = $("ul");
 var attributes = $div.prop("attributes");
 // loop through <select> attributes and apply them on <div>
 $.each(attributes, function() {
   $ul.attr(this.name, this.value);
 });

http://jsfiddle.net/eSrv9/