function copyAttributes (from, to, opts) {
var attr, _i, _len, _ref, _ref1, _results;
if(!Array.isArray(opts)){
console.error("copyAttributes的第三个参数必须为数组")
opts = "";
}else{
opts = opts.join();
}
_ref = from.attributes;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attr = _ref[_i];
_ref1 = attr.nodeName;
if ((opts != "") && (opts.indexOf(_ref1) >= 0)) {
continue;
}
_results.push(to.setAttribute(attr.nodeName, attr.nodeValue));
}
return _results;
}
使用方法
let from = document.getElementById('from')
let to = document.getElementById('to')
//除了src属性不复制全部复制一遍
copyAttributes(from, to, ['src'])