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();
});
精彩评论