How to solve the problem of events with dynamic binding missing after innerHTML _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.