如何将一个元素的属性复制到另一个元素?
HTML
<select id="foo" class="bar baz" style="display:block" width="100" data-foo="bar">...</select>
<div>No attributes yet</div>
JavaScript
var $div = $('div');
var $select = $('select');
//now copy the attributes from $select to $div
最佳答案
您可以使用 native 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"));
关于javascript - 如何复制一个元素的所有属性并将它们应用到另一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6753362/