[removed] Adding an onClick handler without overwriting the existing one

前端 未结 7 1154
说谎
说谎 2020-12-14 02:54

I\'m trying to modify all links on a page so they perform some additional work when they are clicked.

A trivial approach might be something like this:



        
7条回答
  •  囚心锁ツ
    2020-12-14 03:21

    You need to create a closure to preserve the original onclick value of each link:

    Hi
    There
    
    

    Note that this implementation only performs the new onclick handling if the original onclick handler returns true. That's fine if that's what you want, but keep in mind you'll have to modify the code slightly if you want to perform the new onclick handling even if the original handler returns false.

    More on closures at the comp.lang.javascript FAQ and from Douglas Crockford.

提交回复
热议问题