在下面的代码中为什么on事件处理程序不启动?(In the below code Why doesn't the on event Handler fire up?)

编程入门 行业动态 更新时间:2024-10-11 07:34:14
在下面的代码中为什么on事件处理程序不启动?(In the below code Why doesn't the on event Handler fire up?)

为什么我没有得到控制台输出,是否有一些概念错误HTML页面是

!DOCTYPE html> <html> <head> <title>prac 1</title> <script type="text/javascript" src="jquery-3.1.1.min (1).js"></script> <script type="text/javascript" src="script2.js"></script> </head> <body> <input type="text" id="just"> <input type="submit" id="submit"> <p></p><p></p> <div id="div1"></div> </body> </html>

另外script2.js是

$(document).on('ready',check()); function check(){ $('#submit').on('click',function(){ console.log("hi"); }) }

谢谢sdcsc = dsfsdfvdvd(只是为了消除细节的错误)

Why am I not getting a console output,Is there some conceptual mistake The HTML page is

!DOCTYPE html> <html> <head> <title>prac 1</title> <script type="text/javascript" src="jquery-3.1.1.min (1).js"></script> <script type="text/javascript" src="script2.js"></script> </head> <body> <input type="text" id="just"> <input type="submit" id="submit"> <p></p><p></p> <div id="div1"></div> </body> </html>

Also the script2.js is

$(document).on('ready',check()); function check(){ $('#submit').on('click',function(){ console.log("hi"); }) }

Thanks sdcsc=dsfsdfvdvd(just to eliminate the error of details)

最满意答案

因为check() 调用立即在元素存在之前check ,所以它们没有找到,并且处理程序没有连接。

你应该把一个函数而不是它的返回值传递给ready :

$(document).on("ready", check); // No () --------------------^

Because check() calls check, immediately, before the elements exist, and so they aren't found, and the handler isn't hooked up.

You're supposed to pass a function, not its return value, into ready:

$(document).on("ready", check); // No () --------------------^

更多推荐

本文发布于:2023-07-31 09:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中为   事件   代码   程序   Handler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!