gpt4 book ai didi

javascript - 未捕获的 ReferenceError : functionis not defined error, 为什么?

转载 作者:行者123 更新时间:2023-11-28 02:43:54 24 4
gpt4 key购买 nike

我一直在尝试使用 javascript,它可以工作,但突然停止工作,即 js 函数没有被调用。我已将 .js 和 html 文件放在桌面上,但仍然无法正常工作。

错误:

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>

最佳答案

试试这个......

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>

关于javascript - 未捕获的 ReferenceError : functionis not defined error, 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com