2017-02-21 75 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

唯一的语法错误是'功能'。省略分号不是**语法错误。如果省略分号是好还是不好,你可以争论很多,但两者都有其优点和缺点。 –

相关问题