在下面的代码中为什么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 () --------------------^更多推荐
发布评论