问题
I have a real simple jQuery accordion based on http://www.stemkoski.com/stupid-simple-jquery-accordion-menu/
Everything works fine but I would like it to automatically have the first item in the list open when the page loads
I have everything in a jsfiddle at http://jsfiddle.net/HJ8c7/
Can anyone help?
回答1:
Do:
$( "#accordion" ).accordion( "option", "active", 0 );
It will open the first element.
回答2:
you can just do it by jquery
$(document).ready(function() {
$(".accordionButton:first").trigger("click");
});
js fiddle demo live
jquery trigger mathod is used for trigger the event
.trigger( eventType [, extraParameters] )
Ref: Jquery trigger
回答3:
You can do that pretty easy by triggering the click event. Based on your jsfiddle code:
jQuery('div.accordionButton').click(function() {
jQuery('div.accordionContent').slideUp('normal');
jQuery(this).next().slideDown('normal');
});
jQuery("div.accordionContent").hide();
jQuery('div.accordionButton:eq(0)').trigger('click');
Apart from you original question, you may want to use jquery differently so that you do not have to use "jQuery" all the time. It is common to bind the jquery object to the $ variable:
jQuery(function($) {
$('div.accordionContent:eq(0)').trigger('click');
});
来源:https://stackoverflow.com/questions/12248276/jquery-accordion-open-item-on-page-load