简体   繁体   English

点击页面加载按钮

[英]Click Button on Page load

I am currently trying to implement an accordion like section using buttons.我目前正在尝试使用按钮实现类似手风琴的部分。 If the user clicks on one button, the content shows.如果用户单击一个按钮,则显示内容。 However i want the section of the second button to be shown on default.但是我希望默认显示第二个按钮的部分。 Is there any way to do that?有没有办法做到这一点?

This is my code right now:这是我现在的代码:

 jQuery(function() { jQuery('.ss_button').on('click', function() { jQuery('.ss_content').slideUp('fast'); jQuery(this).next('.ss_content').slideDown('fast'); }); });
 #ss_menu { width: auto; } .ss_button { background-color: #049132; border-bottom: 1px solid #FFFFFF; cursor: pointer; padding: 10px; margin-bottom: 5px; color: #FFFFFF; } .ss_content { background-color: white; display: none; padding: 10px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="ss_menu"> <div class="ss_button">Einsteiger</div> <div class="ss_content"> <p>Test1</p> </div> <div class="ss_button">Mittelklasse</div> <div class="ss_content"> <p>Test2</p> </div> <div class="ss_button">High-End</div> <div class="ss_content"> <p>Test3</p> </div> </div>

You can do it in a few way, 1 could be:您可以通过几种方式做到这一点,1 可能是:

jQuery('.ss_content').eq(1).show();

or you can trigger the click event of the first ss_button或者你可以触发第一个ss_button的点击事件

jQuery('.ss_button').eq(1).click();

DEMO演示

 jQuery(function() { jQuery('.ss_button').on('click', function() { jQuery('.ss_content').not(jQuery(this).next('.ss_content')).slideUp('fast'); jQuery(this).next('.ss_content').slideToggle('fast'); }); jQuery('.ss_content').eq(1).show(); });
 #ss_menu { width: auto; } .ss_button { background-color: #049132; border-bottom: 1px solid #FFFFFF; cursor: pointer; padding: 10px; margin-bottom: 5px; color: #FFFFFF; } .ss_content { background-color: white; display: none; padding: 10px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="ss_menu"> <div class="ss_button">Einsteiger</div> <div class="ss_content"> <p>Test1</p> </div> <div class="ss_button">Mittelklasse</div> <div class="ss_content"> <p>Test2</p> </div> <div class="ss_button">High-End</div> <div class="ss_content"> <p>Test3</p> </div> </div>

As alternative to "click the button to show on page load" you can show the required content by overriding the display:none .作为“单击按钮以在页面加载时显示”的替代方法,您可以通过覆盖display:none来显示所需的内容。

This also has the bonus of not having the FOUC (flash of unstyled content) that you get when running js to setup your page (especially if have a lot of js or a large page that must load before doc.ready can run).这还有一个好处,即没有在运行 js 来设置页面时获得的 FOUC(无样式内容的闪存)(特别是如果有很多 js 或必须在 doc.ready 运行之前加载的大页面)。

Of course, this may not be possible in all cases, so provided as an alternative.当然,这可能并非在所有情况下都是可行的,因此作为替代提供。

 jQuery(function() { jQuery('.ss_button').on('click', function() { jQuery('.ss_content').slideUp('fast'); jQuery(this).next('.ss_content').slideDown('fast'); }); });
 #ss_menu { width: auto; } .ss_button { background-color: #049132; border-bottom: 1px solid #FFFFFF; cursor: pointer; padding: 10px; margin-bottom: 5px; color: #FFFFFF; } .ss_content { background-color: white; display: none; padding: 10px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="ss_menu"> <div class="ss_button">Einsteiger</div> <div class="ss_content"> <p>Test1</p> </div> <div class="ss_button">Mittelklasse</div> <div class="ss_content" style='display:block;'> <p>Test2</p> </div> <div class="ss_button">High-End</div> <div class="ss_content"> <p>Test3</p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM