0
剛開始JavaScript,並試圖通過簡單的練習來教自己。我已經做出了這個非常簡單的小程序,它按預期工作:JavaScript的基本功能
<!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
<script type="text/javascript">
function doSomething(){
alert("you clicked the text");
}
</script>
</head>
<body>
<p id="text" onclick="doSomething()"> This is some text </p>
</body>
</html>
當您單擊文本時,將出現警報。然後,我稍微修改了它這樣的:
<!DOCTYPE html>
<html>
<head>
<title>Exercise 2</title>
<script type="text/javascript">
var whenClicked = document.getElementById("text");
whenClicked.onclick = doSomething();
function doSomething(){
alert("you clicked the text");
}
</script>
</head>
<body>
<p id="text"> This is some text </p>
</body>
</html>
期待它以同樣的方式工作完全,但是當我加載在Web瀏覽器中的文件,該警報出現直線距離,沒有我點擊文本。這是爲什麼發生?謝謝P.
(一旦你解決這個問題,你的下一個課題進行說明[這裏](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element)) – Quentin
#Skillz @Quentin :) – Liam
偉大的@Quentin提到什麼將是他的直接下一個問題 –