`.click(handler())`和`.click(handler)`之间的区别

编程入门 行业动态 更新时间:2024-10-10 19:16:44
本文介绍了`.click(handler())`和`.click(handler)`之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript" src="jquery-1.4.2.min.js" ></script> <script type="text/javascript"> function myFun() { alert(5) } $(document).ready(function(){ $("#myID").click(myFun()); }) </script> </head> <body> <input type="button" value="Click it" id="myID" /> </body> </html>

当我运行此代码时, alert(5)即将到来。如果我写

When I run this code then alert(5) is coming when the page loads. If I write

$("#myID").click(myFun)

然后只有当我们点击按钮时才会出现警报。为什么它会这样?

then the alert only appears when we click on the button. Why does it behave like this?

推荐答案

$("#myID").click(myFun());

这会调用 myFun()并尝试安装它返回的任何事件处理程序。由于它没有返回任何内容,实际上触发点击 #myID 。

This calls myFun() and tries to install whatever it returns as an event handler. Since it is not returning anything, you are actually triggering the click on #myID.

更多推荐

`.click(handler())`和`.click(handler)`之间的区别

本文发布于:2023-11-27 09:44:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   click   handler

发布评论

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

>www.elefans.com

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