简体   繁体   English

如何触发点击页面加载?

[英]How to trigger click on page load?

I'm looking for a way to automatically "click" an item when the page loads.我正在寻找一种在页面加载时自动“单击”项目的方法。

I've tried using我试过使用

$("document").ready(function() {
    $("ul.galleria li:first-child img").trigger('click');
});

but it doesn't seem to work?但它似乎不起作用? However, when I enter $("ul.galleria li:first-child img").trigger('click');但是,当我输入$("ul.galleria li:first-child img").trigger('click'); into Firebug's console and run the script, it works.进入 Firebug 的控制台并运行脚本,它可以工作。

Can the trigger event be used on load?可以在加载时使用触发事件吗?

The click handler that you are trying to trigger is most likely also attached via $(document).ready()<\/code> .您尝试触发的点击处理程序很可能也通过$(document).ready()<\/code>附加。 What is probably happening is that you are triggering the event before the handler is attached.可能发生的情况是您在附加处理程序之前触发了事件。 The solution is to use setTimeout<\/code> :解决方案是使用setTimeout<\/code> :

$("document").ready(function() {
    setTimeout(function() {
        $("ul.galleria li:first-child img").trigger('click');
    },10);
});
$(function(){

    $(selector).click();

});
$("document").ready({
    $("ul.galleria li:first-child img").click(function(){alert('i work click triggered'});
}); 

$("document").ready(function() { 
    $("ul.galleria li:first-child img").trigger('click'); 
}); 

just make sure the click handler is added prior to the trigger event in the call stack sequence.只需确保在调用堆栈序列中的触发事件之前添加点击处理程序。

  $("document").ready(function() { 
        $("ul.galleria li:first-child img").trigger('click'); 
    }); 

   $("document").ready({
        $("ul.galleria li:first-child img").click(function(){alert('i fail click triggered'});
    }); 

try this,试试这个,

$("document").ready(function(){

$("your id here").trigger("click");
});

You can do the following :-您可以执行以下操作:-

$(document).ready(function(){
    $("#id").trigger("click");
});

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

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