开发者

Execute click event on load

开发者 https://www.devze.com 2023-04-05 15:34 出处:网络
I have a click event function like so: $(\'.expand2\').click(function(e) { alert(\'in\'); e.preventDefault();

I have a click event function like so:

        $('.expand2').click(function(e) {
            alert('in');
            e.preventDefault();
            var obj = $(this);
...

I need to execute it after page load, as well as clicking on the link with class="expand2".

How would i simulate a click event right after the page load for the first on the for开发者_JS百科m with a class "expand2"??

I've tried this:

$(function(){
    $('.expand2:first').click();
}

didn't work.


$(document).ready(function() { ... }) is just shorthand for $(function() { ... }) so that shouldn't be your problem. Provided you've copied the code correctly, it just looks as though you forgot to close your brackets

EDIT: roXon's demo with $(function(){}) just to show you it works the same http://jsfiddle.net/rWbUD/1/


DEMO

$('.expand2').click(function(){
   alert('clicked')
});

$(document).ready(function() {
    $('.expand2:first').click();
})


Have you tried this:

$(document).ready(function() {
    $('.expand2:first').click();
})

See the documents for this.

If this doesn't work can you provide a jsFiddle demonstrating your problem?


$(document).ready(function() {
   $('.expand2:first').click();

});


Use $(document).ready()

$(document).ready(function(){
       $('.expand2:first').click();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消