If you use innerHTML to retrieve a piece of content and then return to innerHTML, the events that were originally dynamically bound will be lost. We will share with you the solution below, if you are interested, you can refer to the following link and use innerHTML to retrieve a piece of content before returning innerHTML. Then, the events that were originally dynamically bound will be lost, for example:
Html:
The Code is as follows:
Click
Script:
The Code is as follows:
Document. getElementById ('d1 '). onclick = function () {alert (1 )};
Var html = document. body. innerHTML;
Document. body. innerHTML = html;
After this code is executed, clicking d1 does not respond.
Solution:
Bind onclick to the parent element, and use the bubble principle to determine whether the current element is d1. If it is d1, execute
The Code is as follows:
Document. body. onclick = function (e ){
Var e = e | event;
Var currentequale.tar get | e. srcElement
If (current. id = 'd1 ') {alert (1 )}
}
This is also a compromise and will definitely affect the efficiency.