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

标签 javascript html backend

我一直在尝试使用 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/

相关文章:

javascript - 在 javascript 中访问 js 变量时无法读取 null 的属性 'value'

javascript - Node js 数组仪表板

python - 如何在谷歌应用程序引擎中使用后端而不浪费CPU资源?

javascript - 尝试测试数组中是否存在某个项目并更新数组

javascript - 如何通过 url 传递 2 个 JavaScript 变量?

html - css div 占据整个屏幕

javascript - 窗口处于事件状态时,前端停止更新元素

javascript - 如何使 'SpriteSpin' 加载特定图像作为加载的第一个图像?

javascript - react : How to manage state for multiple input types

php - 数组 : Notice: Undefined offset: 0 的问题