How to change onClick handler dynamically?

前端 未结 11 1589
清歌不尽
清歌不尽 2020-12-02 15:37

I\'m sure there are a million posts about this out there, but surprisingly I\'m having trouble finding something.

I have a simple script where I want to set the onC

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 15:47

    If you want to pass variables from the current function, another way to do this is, for example:

    document.getElementById("space1").onclick = new Function("lrgWithInfo('"+myVar+"')");
    

    If you don't need to pass information from this function, it's just:

    document.getElementById("space1").onclick = new Function("lrgWithInfo('13')");
    

提交回复
热议问题