简体   繁体   English

jQuery ui手风琴避免关闭项目wnen单击另一项

[英]jquery ui accordion avoid closing item wnen clicking on another one

starting from existing fiddle, I've created this sample: http://jsfiddle.net/2DaR6/90/ 从现有的小提琴开始,我创建了此示例: http : //jsfiddle.net/2DaR6/90/

Here's the html code: 这是html代码:

<div id="accordion">
  <h3 id="header1" class="accClicked"><a href="#">Section 1</a></h3>
    <div> Good Morning Stackoverflow</div>
  <h3 id="header2" class="accClicked"><a href="#">Section 2</a></h3>
    <div>Buongiorno Stackoverflow</div>
  <h3 id="header3" class="accClicked"><a href="#">Section 3</a></h3>
    <div>Bonjour Stackoverflow</div>
</div>

and here's the js code: 这是js代码:

$(function() {
    var icons = {
        header: "ui-icon-circle-arrow-e",
        headerSelected: "ui-icon-circle-arrow-s"
    };
    $( "#accordion" ).accordion({
        icons: icons,
        collapsible: true
    });
    $( "#header1" ).click(function() {
        $( "#accordion" ).accordion( "option", "icons", false );
    }, function() {
        $( "#accordion" ).accordion( "option", "icons", icons );
    });
});​

If I click on Section 1, accordion correctly opens, but I want that clicking on other items, previously opened items won't close. 如果单击第1节,手风琴将正确打开,但是我希望单击其他项,则以前打开的项不会关闭。 How can I obtain this behaviour? 我如何获得这种行为? Thanks 谢谢

You should not use jquery accordion for this kind of purpose. 您不应将jquery手风琴用于这种目的。 However, its relatively easy to override any element events behaviour. 但是,它相对容易覆盖任何元素事件行为。 When accordion is initialized, you just have to override click handler to corresponding elements. 初始化手风琴后,您只需要将click handler覆盖到相应的元素即可。

In your case, this giving something like this: 您的情况如下:

$('#accordion .accClicked')
        .off('click')
    .click(function(){
        $(this).next().toggle('fast');
    });​

See working jsFiddle 参见工作jsFiddle

I agree that accordion probably isn't the best plugin to use, BUT you could do the following: 我同意手风琴可能不是最好的插件,但是您可以执行以下操作:

You could change from using an id and use accordion as a class, and then have multiple div to break your sections up. 您可以更改使用id并将手风琴用作类,然后使用多个div来拆分您的部分。

 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <script> $(function() { $( ".accordion" ).accordion({collapsible: true, active: false}); }); </script> <div class="accordion"> <h3>header 1</h3> <p>this is my content 1</p> </div> <div class="accordion"> <h3>header 2</h3> <p>this is my content 2</p> </div> 

May be do you need another plugin collapse/expand? 可能您需要另一个插件折叠/展开吗? For example this (see the example of the bottom of page) 例如 (见页面底部的例子)

$(".xyz").accordion({ collapsible: true, $(“。xyz”)。accordion({ 可折叠:true,
active: false, heightStyle: "content", icons:"", }); 主动:false, heightStyle:“内容”,图标:“”,});

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

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