复制Dom元素

xiaoxiao2022-06-03  47

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'])

 

转载请注明原文地址: https://www.6miu.com/read-4915059.html

最新回复(0)