我是JQuery/Javascript的新手。有沒有一種很好的方法將一堆元素放在數組中並循環,調用一個函數來執行某些操作。讓我解釋我正在嘗試做什麼。Jquery - 循環遍歷數組DIVs,顯示並隱藏它們
我有下面的代碼將隱藏並顯示2個DIV當鼠標點擊DIV外部。它效果很好。但是,我有很多DIV顯示和隱藏。請告訴我一種放入DIV數組的方法,將下面的代碼轉換爲函數ShowHideDIV(hideDiv,showDiv)來執行操作?
$(document).mouseup(function (e)
{
var showContainer1 = $("#divShipMethod");
var hideContainer1 = $("#divShipMethodDDL");
if (!hideContainer1.is(e.target)
&& hideContainer1.has(e.target).length === 0)
{
hideContainer1.hide();
showContainer1.show();
}
});
例如,說我有以下DIV ID的喜歡的陣列把:
- divShow1
- divHide1
- divShow2
- divHide2
- divShow3
- divHide3
- divShow4
- divHide4
- divShow5
- divHide5
你可以用'$。 each()'用jQuery循環訪問數組。 – Barmar
您可能根本不需要循環,不同類型的div中的常見類名可能會更容易維護。 –
如何將這些DIV放入數組中並循環使用?我是Jquery的新手,代碼示例將不勝感激。謝謝 – milacay