Bạn có thể sử dụng getEventListeners
trên các nút? Không biết hỗ trợ là gì hoặc nếu nó chỉ được hỗ trợ trong bảng điều khiển?
function cloneMassive(node) {
// Clone the node, don't clone the childNodes right now...
var dupNode = node.cloneNode(false);
var events = getEventListeners(node);
for(var p in events) {
// All events is in an array so iterate that array:
events[p].forEach(function(ev) {
// {listener: Function, useCapture: Boolean}
dupNode.addEventListener(p, ev.listener, ev.useCapture);
});
}
// Also do the same to all childNodes and append them.
if (node.childNodes.length) {
[].slice.call(node.childNodes).forEach(function(node) {
dupNode.appendChild(cloneMassive(node));
});
}
return dupNode;
}
var dupBody = cloneMassive(document.body);
Nhưng dường như getEventListeners
là không thực sự được hỗ trợ:
Get event listeners attached to node using addEventListener
Nếu bạn cần phải sao chép tất cả các thuộc tính sự kiện vào nút cũng bạn sẽ cần một danh sách tất cả, và sau đó chỉ cần sao chép chúng qua:
['onclick', 'onmouseover', '...'].forEach(function(method) {
dupNode[method] = node[method];
});
Có thể trùng lặp [cách sao chép nút DOM với trình xử lý sự kiện?] (Http://stackoverflow.com/questions/15408394/how-to-copy-a -dom-node-with-event-listeners) – Nickolay