Problem analysis: this error occurs when the JS file is placed in the head tag and the onclick event is bound
reason:
W3school introduces that the browser loads the button node before executing JS. When the browser parses from top to bottom, the button node bound to onclick cannot be found
For example:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Events</title>
<script>
var Btn = document.getElementById('btn');
Btn.onclick = function(){
console.log("push the button ");
}
</script>
</head>
<body>
<button id="btn">Calculation</button>
</body>
</html>
This error will appear, as shown in the following figure:
Solution 1: use the JS content in window ο nl ο Ad = function () {} wrap it up
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Events</title>
<script>
window.onload = function () {
var Btn = document.getElementById('btn');
Btn.onclick = function () {
console.log("push the button ");
}
}
</script>
</head>
<body>
<button id="btn">Calculation</button>
</body>
</html>
Solution 2: load the JS file at the bottom
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Events</title>
</head>
<body>
<button id="btn">Calculation</button>
<script>
var Btn = document.getElementById('btn');
Btn.onclick = function () {
console.log("push the button ");
}
</script>
</body>
</html>
Read More:
- [Solved] Binding onclick event in JS: for loop: error uncaught typeerror: cannot set properties of undefined (setting ‘classname’)
- JS bug Log Uncaught TypeError: Cannot read property ‘previoustSibling‘ of null
- [Solved] JS Error: Uncaught TypeError: Cannot set properties of null (setting ‘innerHTML‘)
- [Solved] Uni.createintersectionobserver Error: Uncaught TypeError: Cannot read property ‘bottom’ of null
- [Solved] Error in callback for watcher “value“: “TypeError: Cannot read property ‘level‘ of null“
- [Solved] electron Error: Uncaught TypeError: Cannot read property ‘BrowserWindow‘ of undefined
- [Solved] Vue Error: Uncaught TypeError: Vue.createApp is not a function
- [Solved] Syntax Error: TypeError: Cannot set property ‘parent‘ of undefined
- vue Error: Uncaught SyntaxError: Invalid shorthand property initializer
- Uni-app Error when assigning a value to a component: [system] TypeError: Cannot read property ‘name‘ of undefined
- Asynchronous loading method of Baidu map
- [Solved] Echarts Error: TypeError: Cannot read properties of null (reading getAttribute )
- [Solved] JS Error: cannot set property xxx of undefined
- [Solved] react Chrome Browser Error: Uncaught TypeError: Cannot read properties of undefined (reading ‘forEach‘)
- [Solved] Syntax Error: TypeError: Cannot read property ‘parseComponent‘ of undefined
- [Web Browser] “Uncaught TypeError: object is not a function“
- [Solved] TYPEERROR: CANNOT READ PROPERTY ‘REDUCE‘ OF UNDEFINED
- About JS error uncaught syntax error: invalid shorthand property initializer
- Echarts Partially introduced error: TypeError: Cannot read property ‘findAxisModel’ of undefined
- [Solved] Uncaught (in promise) TypeError: XXX.a is not a constructor