我想寫一個腳本來在網站上創建'夜間模式'。爲什麼我會得到「SyntaxError:Expected token']'」?
每當我試着運行該腳本,我得到的錯誤
"SyntaxError: Expected token ']'" on line 9.
這行說:
this.pageElements[0] = ['element': document.body, 'background': ['day': '#f1f1f1', 'night': '#0e0e0e']];
爲什麼不工作?
這是全碼:
window.nightMode = {};
//Functions
nightMode.init = function() {
this.pageElements = [];
//Format: ['element': Element, 'background': ['day': Day background color, 'night': Night background color], 'text': ['day': Day text color, 'night': Night text color]];
this.pageElements[0] = {
'element': document.body,
'background': {
'day': '#f1f1f1',
'night': '#0e0e0e'
}
};
this.pageElements[1] = {
'element': document.getElementById('yt-masthead-container'),
'background': {
'day': '#fff',
'night': '#000'
}
};
this.pageElements[2] = {
'element': document.getElementById('search-btn'),
'background': {
'day': '#fff',
'night': '#000'
}
};
this.pageElements[3] = {
'element': document.getElementById('masthead-search-terms'),
'background': {
'day': '#fff',
'night': '#000'
},
'text': {
'day': '#666',
'night': '#999'
}
};
this.isNight = false;
}
nightMode.setNight = function() {
for (var i = 0; i < this.pageElements.length; i++) {
this.pageElements[i].element.style.background = this.pageElements[i].background.night
if (this.pageElements[i].text) {
this.pageElements[i].element.style.color = this.pageElements[i].text.night
}
}
alert('Night mode');
this.isNight = true;
}
nightMode.setDay = function() {
for (var i = 0; i < this.pageElements.length; i++) {
this.pageElements[i].element.style.background = this.pageElements[i].background.day
if (this.pageElements[i].text) {
this.pageElements[i].element.style.color = this.pageElements[i].text.day
}
}
alert('Day mode');
this.isNight = false;
}
nightMode.toggle = function() {
if (this.isNight) {
this.setDay();
} else {
this.setNight();
}
}
//Startup Code
nightMode.init();
nightMode.toggle();
更新到:'this.pageElements [0] = { '元件':document.body的, '背景':[ '天': '#F1F1F1', '夜間':」 #0e0e0e']};' – Jack
可能因爲這是無效的,數組沒有鍵和值。 – adeneo