2017-04-06 90 views
0

我遇到了展開/摺疊菜單的問題。在菜單上,我試圖做兩件事:首先是展開/摺疊所有。這工作得很好。作爲參考,我的代碼如下:展開/摺疊所有人與個人

$("#expandAll dt a").click(function() { 
       $("dd").slideToggle(); 
      }); 

我只是要求它將slidetoggle函數應用於所有dd標記。

我還想在列表中展開一個項目,而其餘項目保持摺疊狀態。但是,當我這樣做時,它會擴展整個列表,除了我想要的那個,我正在尋找完全相反的行爲。對於單件的代碼如下:

function excol() { 
    $("#expand dt a").click(function() { 
     $(this).parent().siblings("dd").slideToggle(); 
    }); 
} 

我的「這個」說法應改爲「切換上/下與標籤DD是此元素的父的兄弟姐妹一個單一的元素。」問題是,它會切換每一個dd,除了我想要切換的那個。

下面是相關的jQuery代碼:

 //Upon successful ajax call 
    success: function (data) { 


     excol(); 

     $("#expandAll dt a").click(function() { 
      $("dd").slideToggle(); 
     }); 
    }, 

}); 

} 

function excol() { 
$("#expand dt a").click(function() { 
    $(this).parent().siblings("dd").slideToggle(); 
}); 
} 

下面是HTML代碼:

<div id="expandAll"> 

       <dl> 
        <dt> 
         <a style="font-size: 12px; color: black;"  href="#">Expand/Collapse All</a> 
        </dt> 
       </dl> 

       <div id="expand"> 
        <div id="mhsPrograms"> 
         <div id="MHS"> 
         </div> 
        </div> 
        <br /> 
       </div> 
</div> 

我覺得我很接近解決方案,但有衝突某處,我」米沒有看到。任何幫助,將不勝感激。

+0

編輯:兩種功能工作時,我分別嘗試出來。但是,當我將擴展全部包含在內時,問題就開始了,因此可能會出現我沒有看到的衝突。 – Rumble1701a

回答

0

Got it!問題在於展開全部函數的位置。我沒有把所有的代碼放在這裏,因爲我認爲它沒有關係......我現在看到了我的方式的錯誤,並將發佈一切。無論如何,我需要將展開/摺疊全部功能放到我的文檔就緒功能中。所以我的代碼如下所示:

$(document).ready(function() { 
//Call process function 
processListItems(Url, Listname); 
excolAll(); 
//************************* 
//processListItems function 
//************************* 


}); 

exolAll()是問題函數。我仍然不完全理解這些操作,但它現在可行。

完整的代碼如下供參考:

$(document).ready(function() { 
//Call process function 
processListItems(Url, Listname); 
excolAll(); 
//************************* 
//processListItems function 
//************************* 


}); 

function processListItems(url, listname, complete, failure) { 
// Executing our items via an ajax request 
$.ajax({ 
    url: url + "/_api/web/lists/getbytitle('" + listname + "')/items? $orderby=SortOrder", 
    method: "GET", 
    headers: { "Accept": "application/json; odata=verbose" }, 
    //Upon successful ajax call 
    success: function (data) { 

     ewiArray(data); 
     excol(); 
     //excolAll(); 
     joinAll(); 

    }, 
}); 
} 

//complete(data);// Returns JSON collection of the results     

//Function for expanding/collapsing menu 
function excol() { 
$("#expand dt a").click(function() { 
    $(this).parent().siblings("dd").slideToggle(); 
}); 
} 

//Function for expanding/collapsing all 
function excolAll() { 
$("#expandAll dt a").click(function() { 
    $("dd").slideToggle(); 
}); 
} 

//Function for joining EWI program to its associated description 
function joinAll() { 
$("button").click(function() { 
    var fired_button = $(this).val(); 
    sessionStorage.setItem('ewiNum', fired_button); 
    window.location.href = "Application.aspx"; 
    return false; 
}); 
}