javascript - 设置按钮来更改图像 Javascript

标签 javascript html

它应该很简单,但我无法获得在单击按钮时更改图像的功能。请任何人都可以发现下面的代码有什么问题。提前致谢。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>BlankCordovaApp4</title>

<!-- BlankCordovaApp4 references -->
<link href="css/index.css" rel="stylesheet" />
</head>
<body>
<p>Greeting!</p>

<img id="demo" src="images/hello.jpg" width="180" height="123">

<button id="Btn" type="button" onclick="changeImage();">Click Me</button>

<!-- Cordova reference, this is added to your app when it's built. -->
<script src="cordova.js"></script>

<script src="scripts/platformOverrides.js"></script>

<script src="scripts/index.js"></script>


<script>

function changeImage() {

document.getelementbyid("demo").src = "images/bye.jpg";
};

</script>

</body>
</html>

最佳答案

应该是:

document.getElementById("demo").src = "images/bye.jpg";

JavaScript 中的大小写问题:getelementbyidgetElementById 不同。

关于javascript - 设置按钮来更改图像 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28660451/

相关文章:

javascript - onload 将数组中的元素添加到数组

javascript - 更好的设计,以便从对象的方法中引用自己对象的属性

javascript - 用图像替换 CHOOSE FILE

javascript - 在 Firefox 中使用 indexedDB 时出现错误 "Operation failed for reasons unrelated to the database..."

php - 如何为一个站点使用多个网站图标

html - 为什么当 `td` 写入两个 div 而一个设置为 `position: absolute` 和另一个 `position: relative` 时我不能在 td 中设置宽度?

html - 以最大宽度居中绝对定位的 div

python - 无法使用 python 和 beautifulsoup 抓取网页中的某些 href

html - 为什么内容会溢出栏目?

javascript - 如何使用 JavaScript 修改内联 SVG?