jQuery:如何复制一个元素的所有属性并将它们应用到另一个元素? [英] jQuery: How to copy all the attributes of one element and apply them to another?

查看:517
本文介绍了jQuery:如何复制一个元素的所有属性并将它们应用到另一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个元素的属性复制到另一个元素?

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


推荐答案

您可以使用原生 节点#attributes property: 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"));

这篇关于jQuery:如何复制一个元素的所有属性并将它们应用到另一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆