如何触发点击页面加载?

时间:2020-11-30 19:00:48

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'); into Firebug's console and run the script, it works.

但它似乎没有用?但是,当我输入$(“ul.galleria li:first-child img”)。触发器('click');进入Firebug的控制台并运行脚本,它的工作原理。

Can the trigger event be used on load?

触发事件可以在加载时使用吗?

5 个解决方案

#1


99  

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()附加。可能发生的是您在附加处理程序之前触发事件。解决方案是使用setTimeout:

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

A delay of 10ms will cause the function to run immediately after all the $(document).ready() handlers have been called.

延迟10ms将导致函数在调用所有$(document).ready()处理程序后立即运行。

#2


5  

$(function(){

    $(selector).click();

});

#3


4  

$("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'});
    }); 

#4


1  

try this,

尝试这个,

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

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

#5


0  

You can do the following :-

您可以执行以下操作: -

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

#1


99  

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()附加。可能发生的是您在附加处理程序之前触发事件。解决方案是使用setTimeout:

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

A delay of 10ms will cause the function to run immediately after all the $(document).ready() handlers have been called.

延迟10ms将导致函数在调用所有$(document).ready()处理程序后立即运行。

#2


5  

$(function(){

    $(selector).click();

});

#3


4  

$("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'});
    }); 

#4


1  

try this,

尝试这个,

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

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

#5


0  

You can do the following :-

您可以执行以下操作: -

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