[removed] changing the value of onclick with or without jQuery

前端 未结 7 2351
执笔经年
执笔经年 2020-12-07 16:46

I\'d like to change the value of the onclick attribute on an anchor. I want to set it to a new string that contains JavaScript. (That string is provided to the

相关标签:
7条回答
  • 2020-12-07 17:25

    Note that following gnarf's idea you can also do:

    var js = "alert('B:' + this.id); return false;";<br/>
    var newclick = eval("(function(){"+js+"});");<br/>
    $("a").get(0).onclick = newclick;
    

    That will set the onclick without triggering the event (had the same problem here and it took me some time to find out).

    0 讨论(0)
提交回复
热议问题