如果有人可以查看http://jsfiddle.net/fdzVq/ 并借给我一双眼睛,那就太棒了。div 应该更改为提供的消息之一,但它不执行任何操作。
JS:
function getPowerMsg(kj){
var kwh = kj * 0.00027777777777778;
var stuff = [["an LED night light", 0.5],["a clock radio", 4],["a night light", 5]];
for(var i=0; i<stuff.length; i++){
var item = stuff[i][0];
var watts = stuff[i][1];
var kwatts = watts/1000;
var hours = (Math.floor((kwh/kwatts)*2)/2);
if(hours > 0){
results.push([item, convertTime(hours)]);
}
}
return results[randbetween(0,results.length-1)];
}
function convertTime(hrs){
var msg = "";
if(hrs < 24){
msg = hrs+" hour";
if(hrs != 1){ msg += "s"; }
if(hrs < 1){ msg = "a half hour"; }
}else if(hrs == 24){
msg = "a full day";
}else if(hrs < 33){
msg = "over a day";
}else if(hrs < 39){
msg = "about a day and a half";
}else if(hrs < 48){
msg = "almost 2 days";
}else if(hrs == 48){
msg = "2 days";
}else if(hrs < 60 ){
msg = "over 2 days";
}else if(hrs < 72){
msg = "almost 3 days";
}else if(hrs == 72){
msg = "3 days";
}else{
msg = "over 3 days";
}
return msg;
}
function changePowerMsg(num){
var x = document.getElementById("powermsg");
var y = getPowerMsg(num);
x.innerHTML = y;
}
HTML:
<div id="powermsg">Hi</div>
<a href="javascript:;" onclick="javascript: changePowerMsg(200);">Change!</a>