简体   繁体   English

onclick处理程序不执行分配的功能

[英]onclick handler does not execute assigned function

I have this code: 我有以下代码:

<script type="text/javascript">

window.onload = function () {
    document.getElementById('image011').onclick = hideTopMenu();
​}

</script>

In my case, I cant use JQuery, I need to use pure javascript only. 就我而言,我不能使用JQuery,我只需要使用纯JavaScript。
Can someone tell me where the problem is? 有人可以告诉我问题出在哪里吗?
After page load, if I click on image011, the top menu will be hidden. 页面加载后,如果单击image011,则顶部菜单将被隐藏。 Regards 问候

You need to assign a function reference as event handler. 您需要分配一个函数引用作为事件处理程序。

What you are doing is, assign the result from the function call hideTopMenu() 您正在做的是,从函数调用hideTopMenu()分配结果

So, your line needs to look like: 因此,您的行需要看起来像:

document.getElementById('image011').onclick = hideTopMenu;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM