我正在關注本教程https://www.youtube.com/watch?v=b4GwvdhrEQg&t=537s和5:13他的原型工程,但我的原型沒有。我得到這個職位Uncaught TypeError:無法在window.onload處讀取屬性'addEventListener'null null
Uncaught TypeError: Cannot read property 'addEventListener' of null at window.onload (rico-beweeg-oog.html:29) window.onload @ rico-beweeg-oog.html:29
我無法找到任何代碼問題或解決方案的標題解釋了錯誤,我已經到處放置腳本代碼,前後div元素之後,但它只是贏了」工作。
代碼:
<!DOCTYPE HTML>
<html>
<head>
<style>
#rico {
width: 1280px;
height: 1060px;
overflow: hidden;
background-color: #efefef;
cursor: pointer;
}
#ricobox {
width: 75px;
height: 75px;
background-color: rgb(0,255,0);
border-radius: 50%;
border: 15px rgb(255,0,0) solid;
transform: translate3d(50px, 50px, 0);
}
</style>
<script>
window.onload = function() {
var rico = document.getElementById('#ricobox');
var container = document.getElementById('#rico').addEventListener("click", klikPositie, false);
alert();
function klikPositie(e) {
var xPosition = e.clientX;
var yPosition = e.clientY;
var translate3dValue = "translate3d(" + xPosition + "px," + yPosition + "px, o)";
rico.style.transform = translate3dValue;
}
}
</script>
</head>
<body>
<div id="rico">
<div id="ricobox"></div>
</div>
</body>
</html>
使用'''document.getElementById('rico')'''和不帶井號的document.getElementById('ricobox') – trebor