jQuery accordion - Open item on page load

我的未来我决定 提交于 2020-01-15 05:00:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!