我正在尋找一種方法來簡化使用for循環的代碼。任何幫助表示讚賞。簡單縮短我的代碼
我設置了一個系統,當有人點擊鏈接上有日期的鏈接(例如照片存檔)時,打開模式框架中的全部圖像。我有很多不同的日期,每次我創建一個新的日期,我必須在代碼中插入一百萬次,如下所示。也許我可以創建一個包含日期的數組,並在數組中循環並生成下面的代碼。這可能有一個簡單的解決方法,但我是網絡開發的新手。謝謝!!!!
// Get the modal gallery
var gallery161207 = document.getElementById('gallery161207');
var gallery161130 = document.getElementById('gallery161130');
...
var gallery150916 = document.getElementById('gallery150916');
// Get the close button
var closeButton161207 = document.getElementById('closeModal161207');
var closeButton161130 = document.getElementById('closeModal161130');
...
var closeButton150916 = document.getElementById('closeModal150916');
// Get the buttons
var btn161207 = document.getElementById('161207');
var btn161130 = document.getElementById('161130');
...
var btn150916 = document.getElementById('150916');
// When the user clicks on the button, open the modal gallery
function openGallery(source) {
\t // Open the modal gallery depending on what was clicked
\t if (source == '161207')
\t \t gallery161207.style.display = "block";
\t if (source == '161130')
\t \t gallery161130.style.display = "block";
\t ...
\t if (source == '150916')
\t \t gallery150916.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
closeButton161207.onclick = function() {
gallery161207.style.display = "none";
}
closeButton161130.onclick = function() {
gallery161130.style.display = "none";
}
...
closeButton150916.onclick = function() {
gallery150916.style.display = "none";
}
btn161207.onclick = function() { openGallery('161207'); }
btn161130.onclick = function() { openGallery('161130'); }
...
btn150916.onclick = function() { openGallery('150916'); }
window.onclick = function(event) {
\t if (event.target == gallery161207) {
\t \t closeButton161207.onclick();
\t }
\t if (event.target == gallery161130) {
\t \t closeButton161130.onclick();
\t }
\t ...
\t if (event.target == gallery150916) {
\t \t closeButton150916.onclick();
\t }
}
我已經改變你的[tag:java]標籤添加到[tag:javascript]標籤中。請理解這些是兩種完全不同的編程語言,與漢堡包大致相關,如果你錯誤地標記了你的問題喲你不會得到正確的專家來審查它,這可能會傷害你獲得體面的幫助的機會。由於我對JavaScript一無所知,這就是我能爲你做的所有事情,除非希望你很好,並希望你很快得到一個體面的答案。 –
@HovercraftFullOfEels我真的很喜歡你的評論。 –
謝謝!儘管關於語言的一些事情也非常類似,如語法。 – michaeled314