简体   繁体   English

Javascript onclick 需要点击两次 function 才能运行

[英]Javascript onclick needs to be clicked twice for function to run

Javascript onclick needs to be clicked twice for function to run. Javascript onclick 需要单击两次才能运行 function。 I need it to run on the first click, That is my problem.我需要它在第一次点击时运行,这是我的问题。 I've googled and searched for 2 days now without finding a reply that worked or that I understood.我已经用谷歌搜索并搜索了 2 天,但没有找到有效或我理解的回复。

The code for the javascript can be found here: http://enji.se/windows8/js/script.js javascript 的代码可以在这里找到: http://enji.se/windows8/js/script.js

And the site where I want it to work: http://enji.se/windows8/我希望它工作的网站: http://enji.se/windows8/

It's a Windows 8 looking website I'm trying to make.这是我正在尝试制作的 Windows 8 外观网站。 It will only display correctly on Google Chrome and on a resolution higher then 1265x820 and will be awesome on resolutions higher then 1590x920它只会在谷歌浏览器和高于 1265x820 的分辨率上正确显示,并且在高于 1590x920 的分辨率上会很棒

Thank in advance / enji提前致谢/enji

SOLVED解决了

The problem is inside your click handler boxBegin - the click event is received and passed to your method correctly on the first click, but inside boxBegin you are switching behaviour on the value of box.style.display - in IE9 this is initially "", which triggers your close code rather than the open code you want.问题出在您的点击处理程序boxBegin内部 - 在第一次点击时收到点击事件并正确传递给您的方法,但在boxBegin内部您正在切换box.style.display值的行为 - 在 IE9 中,这最初是“”,这会触发您的关闭代码而不是您想要的打开代码。

Change boxBegin to:更改框开始为:

function boxBegin() {
    var box = document.getElementById('boxBegin');
    if (!box.style.display || box.style.display == "none") {
        box.style.display = "block";
    }
    else {
        box.style.display = "none";
    }
}

I googled this when I came upon the same problem.当我遇到同样的问题时,我用谷歌搜索了这个。

I was checking for if window.location.hash was a certain value, then styling my menu tabs accordingly.我正在检查 window.location.hash 是否是某个值,然后相应地设置我的菜单选项卡的样式。

For me, the solution was as simple as running a 10 millisecond delay function, that then ran my main functions.对我来说,解决方案就像运行 10 毫秒延迟 function 一样简单,然后运行我的主要功能。

The problem for me came from a script that checked for a change at the exact same time as the change was made, not before, not after, but at the same time.对我来说,问题来自一个脚本,该脚本在进行更改的同时检查更改,而不是之前,不是之后,而是同时。 This made me have to double click for it to change again, and for the function to notice the change a second time around.这让我不得不双击它才能再次更改,并且让 function 再次注意到更改。

Remember, it's always easier to run even a 5 millisecond delay script when things don't seem to register changes.请记住,当事情似乎没有记录更改时,即使运行 5 毫秒延迟脚本总是更容易。 This has helped me time and time again这一次又一次地帮助了我

As far as I know, onclick registers single click?据我所知, onclick注册单击?

If onclick requires a double click it might be a browser specific issue.如果 onclick 需要双击,则可能是浏览器特定问题。

Also from the MDC docs :同样来自MDC 文档

The click event is raised when the user clicks on an element.当用户点击一个元素时会引发 click 事件。 The click event will occur after the mousedown and mouseup events. click 事件将在 mousedown 和 mouseup 事件之后发生。

Also: simple test case @ JS Fiddle另外:简单的测试用例@JS Fiddle

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

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