如何触发点击页面加载? [英] How to trigger click on page load?
问题描述
我正在寻找一种在页面加载时自动点击某个项目的方法。
I'm looking for a way to automatically "click" an item when the page loads.
我尝试过使用
$("document").ready(function() {
$("ul.galleria li:first-child img").trigger('click');
});
但它似乎不起作用?但是,当我输入 $(ul.galleria li:first-child img)。触发('click');
进入Firebug的控制台并运行脚本时,它可以工作。
but it doesn't seem to work? However, when I enter $("ul.galleria li:first-child img").trigger('click');
into Firebug's console and run the script, it works.
可以在加载时使用触发事件吗?
Can the trigger event be used on load?
推荐答案
您尝试触发的单击处理程序很可能也通过 $(document).ready()
附加。可能发生的是您在附加处理程序之前触发事件。解决方案是使用 setTimeout
:
The click handler that you are trying to trigger is most likely also attached via $(document).ready()
. What is probably happening is that you are triggering the event before the handler is attached. The solution is to use setTimeout
:
$("document").ready(function() {
setTimeout(function() {
$("ul.galleria li:first-child img").trigger('click');
},10);
});
延迟10ms将导致函数在所有 $之后立即运行(文件).ready()
已经调用了处理程序。
A delay of 10ms will cause the function to run immediately after all the $(document).ready()
handlers have been called.
这篇关于如何触发点击页面加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!