JQuery on Click 事件函数调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4895979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:08:42  来源:igfitidea点击:

JQuery on Click event function call

jquery

提问by David Horák

I would like to call a function from the jQuery Click event. I tried the annotation below which doesn't work.

我想从 jQuery Click 事件调用一个函数。我尝试了下面的注释,但它不起作用。

$(".menu_link").click(GetContentAndSlide());

What is the right annotation?

什么是正确的注释?

回答by The Scrum Meister

$(".menu_link").click(GetContentAndSlide);

remove the (), you are executing the function, instead of passing the function reference.

删除(),您正在执行函数,而不是传递函数引用。

Another option would be (in case your function uses the thispointer) to wrap it around with a anonymous function:

另一种选择是(如果您的函数使用this指针)用匿名函数包装它:

$(".menu_link").click(function() {GetContentAndSlide()});

回答by C?t?lin R?doi

Strangely, .click didn't work for me.

奇怪的是, .click 对我不起作用。

What worked was

有效的是

$(".menu_link").on('click', GetContentAndSlide);