在DOM准备好触发/模仿点击事件

时间:2012-06-21 23:05:43

标签: javascript jquery

我有点击这个jQuery,

$("#overflow-cntr img").click(function(){
            $this = $(this);
            $("#bigpic-cntr canvas").remove();  
            $("#bigpic-cntr #bigpic").css("display", "block");
            window.setTimeout(function(){
                EFFECTS[$this.attr("id")]();
                }, 30);
        });

如何在DOM就绪而不是单击时触发此操作?

2 个答案:

答案 0 :(得分:3)

$(document).ready(function(){
    // bind the event and then trigger it immediately
    $("#overflow-cntr img").click(function(){
        $this = $(this);
        $("#bigpic-cntr canvas").remove();  
        $("#bigpic-cntr #bigpic").css("display", "block");
        window.setTimeout(function(){
            EFFECTS[$this.attr("id")]();
            }, 30);
    }).trigger('click');
});

答案 1 :(得分:0)

将函数放在$(document).ready()而不是click():

$(document).ready(function () {
    $this = $("#overflow-cntr img");
        $("#bigpic-cntr canvas").remove();  
        $("#bigpic-cntr #bigpic").css("display", "block");
        window.setTimeout(function(){
            EFFECTS[$this.attr("id")]();
            }, 30);
});