Is it possible to append to innerHTML without destroying descendants' event listeners?

前端 未结 13 1380
悲哀的现实
悲哀的现实 2020-11-21 23:40

In the following example code, I attach an onclick event handler to the span containing the text \"foo\". The handler is an anonymous function that pops up an <

13条回答
  •  执笔经年
    2020-11-22 00:29

    As a slight (but related) asside, if you use a javascript library such as jquery (v1.3) to do your dom manipulation you can make use of live events whereby you set up a handler like:

     $("#myspan").live("click", function(){
      alert('hi');
    });
    

    and it will be applied to that selector at all times during any kind of jquery manipulation. For live events see: docs.jquery.com/events/live for jquery manipulation see: docs.jquery.com/manipulation

提交回复
热议问题