简体   繁体   English

javascript onclick,onload,onmouseenter-基础

[英]javascript onclick, onload, onmouseenter — the basics

One thing that keeps baffling me is how sometimes when I try to assign a jQuery function via an inline attribute ( onclick="bam();" ), there's perhaps only a 50-50 chance that it'll actually fire off. 让我感到困惑的一件事是,有时当我尝试通过内联属性( onclick="bam();" )分配jQuery函数时,实际上只有50到50的可能性会触发。

I know the function is good because if I assign a regular jQuery .on('click', function(){...}) it works perfectly. 我知道该功能很好,因为如果我分配一个常规的jQuery .on('click', function(){...})它会完美地工作。 After reading and re-reading everything I can find online about this, I'm just as ignorant as ever. 阅读并重新阅读我在网上可以找到的所有内容之后,我和以往一样一无所知。

I'm not seeing the patterns that I need to see and it makes me realize I need to find a simple tutorial about this. 我没有看到需要查看的模式,这使我意识到我需要找到关于此的简单教程。 I don't want to learn JavaScript, I just want to learn about this one key area. 我不想学习JavaScript,我只想了解这一关键领域。

Maybe there is an error in the event handler that prevents it from "working" you may think it only gets called 50% of the time but it actually gets called 100% and gives unexpected results 50% of the time. 也许事件处理程序中存在一个错误,阻止了它“运行”,您可能会认为它仅在50%的时间内被调用,但实际上却被100%调用,并在50%的时间内给出了意外结果。

To see if there are errors you can press F12 in Chrome or Firefox with firebug and check the console. 要查看是否有错误,您可以在Chrome或Firefox中使用Firebug按F12,然后检查控制台。 To see if the function gets called the first line in the function could be: console.log("I am called"); 要查看该函数是否被调用,该函数的第一行可能是: console.log("I am called"); whatever happens after that you should at least see that message in the console. 之后发生的任何事情,您至少应该在控制台中看到该消息。

For better design you should never declare what event should be triggered in your html (inline), it should be done in script and here is where jQuery can make you life easier because it's methods will work and behave the same on all browsers. 为了更好的设计,您永远不要声明应该在html中触发什么事件(内联),应该在脚本中完成,这是jQuery使您的生活更轻松的地方,因为它的方法将在所有浏览器中起作用并表现相同。

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

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