我刚开始学习 JavaScript。我只想构建一个图像轮播,但在我的第一行出现错误:
未捕获的类型错误:无法读取 null 的属性“getAttribute”
js:
function changeImage(){
var imageSrc=document.getElementById("image").getAttribute("src");
}
changeImage();
Run Code Online (Sandbox Code Playgroud)
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="hello.js"></script>
<title></title>
</head>
<body>
<div id="button_left">
<img id ="left" src="left.png">
</div>
<div id="button_right">
<img id ="right" src="right.png">
</div>
<div class="container">
<img id ="image" src="1.jpg">
</div>
<div id="result"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) javascript ×1