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

前端 未结 7 2352
执笔经年
执笔经年 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;";
    var newclick = eval("(function(){"+js+"});");
    $("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).

提交回复
热议问题