我正在学习如何为Web开发做代码,而且我遇到了一个问题。两行几乎完全相同的代码给了我不同的结果:
for(i=0; i<=lunarSpells.length; i++){
document.getElementById(lunarSpells[i]).innerHTML =
Math.ceil(xpLeft/document.getElementById(lunarSpells[i]+"XP").innerHTML);
}
for(i=0; i<=standardSpells.length; i++){
document.getElementById(standardSpells[i]).innerHTML =
Math.ceil(xpLeft/document.getElementById(standardSpells[i]+"XP").innerHTML);
}
不确定它的重要性,但想法是从数组中取一个元素,在文档中找到它,然后相应地填写表格。这是阵列:
var lunarSpells = ["cureOther", "NPCContact", "curePlant", "monsterExamine", "bakePie", "cureMe", "TGMoonclan",
"TPMoonclan", "TGBarbarian", "superglass", "TPKhazard", "TGKhazard", "dream", "stringJewellery",
"statRestorePotShare", "statSpy", "TPBarbarian", "TPWaterbirth", "cureGroup", "TGWaterbirth", "humidify",
"hunterKit", "fertileSoil", "plankMake", "TPCatherby", "TGFishingGuild", "TGCatherby",
"boostPotionShare", "TPIcePlateau", "energyTransfer", "healOther", "TGIcePlateau", "vengeanceOther",
"vengeance", "healGroup", "spellbookSwap", "magicImbue", "TPFishingGuild"]
var standardSpells = ["confuse", "sapphire", "weaken", "bananas", "curse", "lowAlch", "TPVarrock", "emerald", "TPLumbridge", "TPFalador", "TPHouse",
"superheat", "TPCamelot", "ruby", "TPArdougne", "highAlch", "earth", "water", "diamond", "TPWatchtower",
"peaches", "TPTrollheim", "fire", "TPApe", "vulnerability", "air", "dragonstone", "enfeeble", "TOLumbridge", "stun",
"TOFalador", "onyx", "TOCamelot"]
所以我的问题是,为什么lunarspells变量工作,而standardspells变量不是?任何帮助都非常感谢,因为我还是HTML和JS的新手。
答案 0 :(得分:0)
如前所述:
xpLeft/document.getElementById(lunarSpells[i]+"XP").innerHTML
如果document.getElementById("id_that_doesnt_exist")
评估为null
,则将失败(因为null.anything
是错误。
这将停止<script>
块中的所有进一步处理,除非您避免错误,或包裹在try
/ catch
。
所以你的第二个循环将无法到达/执行。