javascript click event handler fires without clicking

前端 未结 3 1507
礼貌的吻别
礼貌的吻别 2020-12-04 02:12

Why does this function get fired without having clicked on the specified button? I had a look at a few similar problems but none deal with this code structure (might be obvi

3条回答
  •  醉话见心
    2020-12-04 02:52

    You are directly calling it.

    document.getElementById("main_btn").addEventListener("click", hideId("main");
    

    You should do that in a callback.

    document.getElementById("main_btn").addEventListener("click", function (){
        hideId("main");
    });
    

提交回复
热议问题