我開始使用JavaScript,通過在網上找到簡單的項目來練習/提高我的JavaScript技能。JavaScript文本遊戲積分系統
這基本上是一個帶有四個按鈕的文本位置遊戲。北,南,東,西。我試圖每次玩家到一個新的位置添加5分。
我該如何做到這一點,一旦按鈕被點擊,它不會添加點?
<script type="text/javascript">
var score = 0;
function north(){
var message = 'You are now in the kitchen';
alert(message);
document.getElementById("score").innerHTML = score + 5;
}
function west(){
var message='You are now the bathroom';
alert(message);
document.getElementById("score").innerHTML = score + 10;
}
function east(){
var message='You are now your bedroom ';
alert(message);
}
function south(){
var message='You are now in the living room';
alert(message);
}
if(document.getElementById('north').clicked == true)
{
document.getElementById('score') = score + 5;
}
else if(document.getElementById('west').clicked == true){
document.getElementById('score') = score + 5;
}
</script>
</head>
<body>
<div id='wrap' />
<header>
<h1> Exploring New Apartment </h1>
</header>
<section>
<article id='textarea'>
<textarea cols='30' rows='5' placeholder='You are currently the living room'></textarea>
<br />
<strong> Score: </strong> <p id='score' style="display:inline">0</p>
</article>
<article>
<div>
<input type='button' value="north" onclick='north()' id='north'/>
<br />
<input type='button' value='west' onclick='west()' />
<input type="button" value='east' onclick='east()'/>
<br />
<input type='button' value='south' onclick='south()'/>
</article>
</section>
<footer>
<p> <a href="mailto:[email protected]"> Contact Us Via Email </a>
</footer>
</div>
</body>
</html>
你似乎沒有在任何地方定義'score'變量。 – 2015-02-17 20:28:35