1. 探测对象,可用于判断浏览器是否支持该方法
window.onload = initAll;
function initAll() {
if (document.getElementById) {
for (var i=0; i<24; i++) {
setSquare(i);
}
}
else {
alert("Sorry, your browser doesn't support this script");
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var newNum = Math.floor (Math.random() * 75) + 1;
document.getElementById(currSquare).innerHTML = newNum;
}
?if (document.getElementById) {
如果对象存在,if语句就为true,脚本继续执行。但是,如果浏览器不理解这个对象,测试就返
回false,并执行条件语句的else部分。
