Jquery dynamically changes the onclick property to cause invalidation

  • 2020-03-30 00:44:30
  • OfStack

 
<li id="" onclick =""></li> 

While this is highly discouraged in many cases, there are other times when you will encounter this code.

At some point you may need to modify the onclick property as follows:
 
$("#id").attr("onclick",url); 

This code, in chrome and firefox you can get the results you want.

Unfortunately in ie, at least ie7 below is no effect, as for i8 seems to be no effect.

The solution is to use jquery bindings.
 
$("#id").attr("onclick","").click(function(){ 
// 
}); 

You need to clear the value of the onclick property before you go to the bind side.

If you didn't set the onclick property in the first place, you don't need to clear it.
 
$("#id").click(funciton(){ 
// 
}); 

Related articles: