我試圖在下拉選項被選中時觸發一個函數,但我不想在HTML內嵌入JavaScript。出於某種原因,當我運行腳本時,會自動註冊更改/點擊。爲什麼?即使在沒有更改的情況下更新/點擊觸發
的jsfiddle:http://jsfiddle.net/nysteve/QHumL/22/
var time = new Date();
var timestamp = time.toString("hh:mm:ss");
//create color from time stamp and print within div
function timeToHexColor(){
var showlist = document.getElementById("board").innerHTML +=
"#" + timestamp.split(":").join("") + "<br/>";
}
//Print colors based on time interval
function Colors(interval) {
this.interval = interval;
switch (this.interval) {
case 'second':
x = setInterval(timeToHexColor,1000);
setTimeout(stopColors, 5000);
break;
case 'minute':
x = setInterval(timeToHexColor,60000);
setTimeout(stopColors, 5000);
break;
case 'hour':
x = setInterval(timeToHexColor,60000*60);
setTimeout(stopColors, 5000);
break;
case 'day':
x = setInterval(timeToHexColor,60000*1440);
setTimeout(stopColors, 5000);
break;
default:
}
}
//For demo purposes manually kill priting after 5 seconds
function stopColors() {
clearInterval(x);
}
//Activate printing by selecting an option.
function generateColors(interval){
document.getElementById("options").onclick = Colors(interval);
/*same result with onchange
I even sent the JSFiddle settings per this link:
http://bit.ly/1gev7zR*/
}
generateColors('second');
這是http://stackoverflow.com的副本/ questions/3249128/javascript-event-handler-arguments =>查看您的問題的答案。 – Jealie