简体   繁体   English

外部按钮打开/关闭手风琴

[英]External Button to open/close Accordion

I need help to open or close accordion with extra buttons. 我需要额外的按钮来打开或关闭手风琴的帮助。

Here is example of the accordion with buttons: 这是带有按钮的手风琴的示例:

 $(function() { $( "#tab1" ).accordion({ collapsible: true, active: false, heightStyle: "content" }); $( "#tab2" ).accordion({ collapsible: true, active: false, heightStyle: "content" }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> <div id="tab1"> <h3>Tab 1</h3> <div>1234567890 Text</div> </div> <div id="tab2"> <h3>Tab 2</h3> <div>1234567890 Text 2</div> </div> <button><span>Open/Close Tab1</span></button> <button><span>Open/Close Tab2</span></button> 

Thank you for your help! 谢谢您的帮助!

You can trigger the click event manually: 您可以手动触发click事件:

 $(function() { $( "#tab1" ).accordion({ collapsible: true, active: false, heightStyle: "content" }); $( "#tab2" ).accordion({ collapsible: true, active: false, heightStyle: "content" }); $('button').first().click(function(){ $('h3').first().click(); }); $('button').last().click(function(){ $('h3').last().click(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> <div id="tab1"> <h3>Tab 1</h3> <div>1234567890 Text</div> </div> <div id="tab2"> <h3>Tab 2</h3> <div>1234567890 Text 2</div> </div> <button><span>Open/Close Tab1</span></button> <button><span>Open/Close Tab2</span></button> 

Use "active" from the accordion's API: http://api.jqueryui.com/accordion/#option-active . 从手风琴的API使用“主动”: http : //api.jqueryui.com/accordion/#option-active

 $(function() { $( "#tab1" ).accordion({ collapsible: true, active: false, heightStyle: "content" }); $( "#tab2" ).accordion({ collapsible: true, active: false, heightStyle: "content" }); $('.toggle-tab').on('click', function(){ var $accordion = $('#tab' + $(this).data('tab')); var state = $accordion.accordion('option', 'active'); $accordion.accordion('option', {active: state === 0 ? false : 0 }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> <div id="tab1"> <h3>Tab 1</h3> <div>1234567890 Text</div> </div> <div id="tab2"> <h3>Tab 2</h3> <div>1234567890 Text 2</div> </div> <button class="toggle-tab" data-tab="1"><span>Open/Close Tab1</span></button> <button class="toggle-tab" data-tab="2"><span>Open/Close Tab2</span></button> 

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

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