簡體   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