2017-02-21 73 views
2

我一直在嘗試我的手在JavaScript上,它的工作原理,但突然停止工作,即js函數沒有被調用。我在桌面上放置了.js和html文件,但仍然無法正常工作。未捕獲ReferenceError:函數未定義錯誤,爲什麼?

錯誤:

Uncaught ReferenceError: funcLoadImage is not defined 
    at HTMLButtonElement.onclick 

這個錯誤可能是任何功能我打電話。

JS:

function funcLoadImage() { 
    document.getElementById("img1").src = "https://upload.wikimedia.org/wikipedia/commons/3/36/Hopetoun_falls.jpg" 
} 

function funcMath(type, data) { 
    if (type == 1) { 
    document.getElementById("img1").alt = 'Type 1' 
    } 

    if (type == 2) { 
    document.getElementById("img1").alt = 'Type 2' 
    } 
} 

funtion funcDate() { 
    document.getElementById("img1").alt = 'date add' 
} 

HTML代碼:

<!DOCTYPE html> 
<html> 
<head> 

</head> 
<body> 

<img id="img1" src="" alt="No image loaded" width="50%" height="50%"/> 
<button id="btn1" width="20%" height="20%" onclick='funcLoadImage()'>Load Image</button> 
<button id="btn2" width="20%" height="20%" onmouseover="this.style.width='60%'" onmouseout="this.style.width='20%'">Write Doc</button> 
<button id="btn3" width="20%" height="20%" onclick='funcMath(1,4)'>Calculate</button> 
<button id="btn4" width="20%" height="20%" onclick='this.innerHTML=Date()'>Display Date</button> 



<p> 

</p> 
<script src="script1.js"></script> 

</body> 
</html> 
+4

如果這是你的實際代碼,那麼你哈哈在這裏有一個錯誤'funtion funcDate',導致語法錯誤,所以'script1.js'的全部內容都沒有被評估,因爲那個語法錯誤。 –

+0

謝謝先生。把它放在答案框中,我會標記爲答案 – Covert

回答

0

試試這個.............

function funcLoadImage() { 
 
    document.getElementById("img1").src = "https://upload.wikimedia.org/wikipedia/commons/3/36/Hopetoun_falls.jpg"; 
 
} 
 

 
function funcMath(type, data) { 
 
    if (type == 1) { 
 
    document.getElementById("img1").alt = 'Type 1'; 
 
    } 
 

 
    if (type == 2) { 
 
    document.getElementById("img1").alt = 'Type 2'; 
 
    } 
 
} 
 

 
function funcDate() { 
 
    document.getElementById("img1").alt = 'date add'; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 

 
</head> 
 
<body> 
 

 
<img id="img1" src="" alt="No image loaded" width="50%" height="50%"/> 
 
<button id="btn1" width="20%" height="20%" onclick='funcLoadImage()'>Load Image</button> 
 
<button id="btn2" width="20%" height="20%" onmouseover="this.style.width='60%'" onmouseout="this.style.width='20%'">Write Doc</button> 
 
<button id="btn3" width="20%" height="20%" onclick='funcMath(1,4)'>Calculate</button> 
 
<button id="btn4" width="20%" height="20%" onclick='this.innerHTML=Date()'>Display Date</button> 
 

 

 

 
<p> 
 

 
</p> 
 
<script type="text/javascript" src="script1.js"></script> 
 

 
</body> 
 
</html>

+1

解釋爲什麼這解決了OP問題。只是粘貼代碼是沒有用的,除非你說OP的錯誤。 – Icepickle

+0

你的代碼有很多的語法錯誤......就像你沒有在任何語句的末尾加上分號... 自動化分號插入導致很多錯誤..所以試圖把自己... –

+0

唯一的語法錯誤是'功能'。省略分號不是**語法錯誤。如果省略分號是好還是不好,你可以爭論很多,但兩者都有其優點和缺點。 –

相關問題