繁体   English   中英

jQuery UI:手风琴回调

[英]JQuery UI: Accordion callbacks

我需要我的javascript仅在打开手风琴上的某个部分时才执行回调,截至目前,由于我仅使用click函数,因此当我打开或关闭一个部分时它才执行回调。 有没有一种方法可以修改我现有的单击功能,使其仅在给定部分被激活时运行?

我当前的点击功能:

$("a#mimetypes").click(function() {
    $("span#mimetypesthrobber").loading(true, { max: 1500 })
    $.getJSON("../mimetypes", function(data) {
        //callback
    });
});

谢谢!

编辑:

我已经在手风琴的另一部分进行了尝试,但无法正常工作:

$('.ui-accordion').bind('accordionchange', function(event, ui) {
if (ui.newHeader == "Encoders") {
EncodersGet();
}
});

您可以使用“ 更改事件

$('.ui-accordion').bind('accordionchange', function(event, ui) {
  ui.newHeader // jQuery object, activated header
  ui.oldHeader // jQuery object, previous header
  ui.newContent // jQuery object, activated content
  ui.oldContent // jQuery object, previous content
});

并例如访问“ newHeadert”并进行处理

编辑

根据新信息{collapsible:true,active:false}

$(document).ready(function() {
            var $acc = $('#accordion').accordion({ collapsible: true,
                   active : false ,
                   change : function (event, ui)
                   {
                                var index = $acc.accordion( "option", "active");
                    if( index === false){
                                 // all are close
                                }
                                else{
                                 // 0-based index of the open section
                                }

                   }
            });
        });

“ option,active”将返回打开部分的索引;如果所有部分都关闭,则返回“ false”

承办商答案的一个改进:在将索引与false进行比较时使用三重等于,以避免第一个手风琴元素匹配。

if (index === false) {
  // All are closed
} else {
  // 0-based index of the open section
}

请记住,在计算条件时,double equals将执行类型转换。

如果dfault关闭了所有手风琴节,则可以用toggle替换click事件,并使第二个函数简单不执行任何操作。

$("a#mimetypes").toggle(function() {
    $("span#mimetypesthrobber").loading(true, { max: 1500 });
    $.getJSON("../mimetypes", function(data) {
        //callback 
    });
},
function() {
    //do nothing
});

更好的解决方案是将一个类添加到活动部分,并在调用加载之前检查该类。

$("a#mimetypes").click(function() {
    if ($(this).hasClass("active")) {
        $(this).removeClass("active");
    }
    else {
        $(".active").removeClass("active"); //Edit - remove all active classes to account for this section being closed by the opening of another
        $(this).addclass("active");

        $("span#mimetypesthrobber").loading(true, { max: 1500 });
        $.getJSON("../mimetypes", function(data) {
            //callback 
        });
    }
});

暂无
暂无

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

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