简体   繁体   English

如何将已经定义的javascript函数传递给jquery调用?

[英]How do I pass a javascript function I've already defined into a jquery call?

For example, if I have: 例如,如果我有:

function example() {
    alert("example");
}

Then why doesn't this work? 那为什么不起作用呢?

$("h2").click( example() );

Does it have to be defined inline? 是否必须内联定义?

Nope, but the parens is making the function execute. 否,但是parens正在使函数执行。 Use this: 用这个:

$("h2").click( example );

you want to just do $("h2").click(example); 您只想做$("h2").click(example); - example should have no parenthesis -示例应没有括号

function example() {
    alert("example");
}

$("h2").click(example);

just use 只是使用

$("h2").click(example);

or 要么

$("h2").click(function() { example(); });

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

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