Javascript onclick

后端 未结 4 1960
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-16 14:42

I have code like

test

Later on code is added to the same anchor tag like

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-16 14:47

    You could try this:

    var lnk = document.getElementById('lnk1'); // don't forget var!
    
    var oldHandler = lnk.onclick;
    lnk.onclick = function(ev) {
      if (oldHandler) oldHandler(ev);
      // do something ...
    };
    

    That code saves a reference to the old handler, and if it's not empty it calls it before doing whatever else the new handler wants to do.

    You could put the call to the old handler after the new code, or mixed in, or whatever.

提交回复
热议问题