如何复制一个元素的所有属性并将其应用于另一元素? [英] How to copy all the attributes of one element and apply them to another?
本文介绍了如何复制一个元素的所有属性并将其应用于另一元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将一个元素的属性复制到另一个元素?
How do I copy the attributes of one element to another element?
HTML
<select id="foo" class="bar baz" style="display:block" width="100" data-foo="bar">...</select>
<div>No attributes yet</div>
JavaScript
JavaScript
var $div = $('div');
var $select = $('select');
//now copy the attributes from $select to $div
推荐答案
您可以使用本机 Node#attributes
属性: http://jsfiddle.net/SDWHN/16/
var $select = $("select");
var $div = $("div");
var attributes = $select.prop("attributes");
// loop through <select> attributes and apply them on <div>
$.each(attributes, function() {
$div.attr(this.name, this.value);
});
alert($div.data("foo"));
这篇关于如何复制一个元素的所有属性并将其应用于另一元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文