我一直在阅读O'Reilly的“Head First HTML5 Programming”。当我到达第70页的示例并完全按照其显示的方式键入代码时,事情似乎出错了。更具体地说,JavaScript代码确实似乎没有执行。我的怀疑在于window.onload属性。
不打算侵犯版权。
<!doctype html>
<html lang="en">
<head>
<title>Phrase-O-Matic</title>
<meta charset="utf-8">
<style>
body { font-family: Verdana, Helvetica, sans-serif; }
</style>
<script>
function makePhrases() {
var words1 = ["24/7", "multi=Tier", "30,000 foot", "B-to-B", "win-win"];
var words2 = ["empowered", "value-added", "oriented", "focused", "aligned"];
var words3 = ["process", "solution", "tipping-point", "strategy", "vision"];
var rand1 = Math.floor(Math.random() * words1.length);
var rand2 = Math.floor(Math.random() * words2.length);
var rand3 = Math.floor(Math.random() * words3.length);
var phrase = words1[rand1] + " " words2[rand2] + " " + words3[rand3];
var phraseElement = document.getElementById("phrase");
phraseElement.innerHTML = phrase; }
window.onload = makePhrases;
</script>
</head>
<body>
<h1>Phrase-O-Matic says:</h1>
<p id="phrase"></p>
</body>
</html>
答案 0 :(得分:3)
这可能是剪切和粘贴错误,但您在行中的字词2之前缺少“+”运算符:
var phrase = words1[rand1] + " " words2[rand2] + " " + words3[rand3];
我能够发现使用Firebug中的JavaScript控制台添加到Firefox中。如果您正在进行HMTL和JavaScript开发,这是一个很好的资源。
答案 1 :(得分:0)
此外,您可以从我的github repo下载该书的所有(希望工作!)代码,以防有:https://github.com/bethrobson/Head-First-HTML5
伊丽莎白