ich einen Fehler in der Konsole haben, der sagt:Warum ist mein JS-Code für zufällige Farbe Generator nicht im Browser arbeiten
Uncaught TypeError: Cannot read property 'addEventListener' of null at (index):14
Hier ist mein Code zu analysieren:
<html>
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Quicksand" />
<head>
<script type="text/javascript">
var div = document.getElementById('text'),
\t \t randomColor = function(e) {
var hex = Math.floor(Math.random() * 0xFFFFFF),
res = e.target,
result = "#" + hex.toString(16);
res.style.backgroundColor = result;
res.innerHTML = result;
\t };
div.addEventListener('mouseover', randomColor);
</script>
<style>
#text{
width: 1000px;
height: 500px;
text-align: center;
font-size: 50px;
font-family: 'Quicksand';
}
body{
text-align: center;
font-family: 'Quicksand';
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
margin-right: 0px;
}
</style>
</head>
<body>
<h1>Hover over me to get a random color!</h1>
<div id="text">Hex code</div>
</body>
</html>
Ich verstehe nicht, weil es in JSFIDDLE funktioniert. Jemand bitte helfen!
Danke Mann, ich habe es jetzt! –