jQuery 调度事件?如何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4308699/
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 17:03:38  来源:igfitidea点击:

jQuery dispatch event? How to

jqueryeventsdispatch

提问by davidino

how can i dispatch an event on my window ready? For example in my code i've got:

我怎样才能在我的窗口上发送一个事件准备好?例如在我的代码中,我有:

$("#info").click(function(){
// do stuff
});

I need to call this function the first time without have a click on #info, in the way // do stuff.

我需要第一次调用这个函数,而无需点击#info,就像 // 做事一样。

回答by Darin Dimitrov

You could use the .trigger()method:

您可以使用以下.trigger()方法:

$('#info').trigger('click');

or simply:

或者干脆:

$('#info').click();

which would be the same.

这是一样的。

回答by Skilldrick

A better way might be to make a function that does the required stuff, then do $(document).ready(myFunc);and $("#info").click(myFunc);.

更好的方法可能是创建一个执行所需内容的函数,然后执行$(document).ready(myFunc);$("#info").click(myFunc);