当前位置:网站首页>Uncaught typeerror cannot set properties of undefined (setting 'classname') reported by binding onclick event in jsfor loop
Uncaught typeerror cannot set properties of undefined (setting 'classname') reported by binding onclick event in jsfor loop
2022-06-25 06:23:00 【m0_ fifty-one million two hundred and seventy-four thousand fou】
If you are little white , This set of information can help you become a big bull , If you have rich development experience , This set of information can help you break through the bottleneck
2022web Full set of video tutorial front-end architecture H5 vue node Applet video + Information + Code + Interview questions .
I want to achieve the following results : Click on the column above , Switch the contents of the following columns 
Write the code as follows ( It mainly depends on js part )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.tab{
display: inline-block;
width: 100px;
height: 50px;
background-color: #aaa;
}
.current{
background-color: yellow;
}
.content{
display: none;
}
</style>
</head>
<body>
<div class = "tab">
<div class = "tab_list">
<li> The column 1</li>
<li> The column 2</li>
<li> The column 3</li>
</div>
<div class = "tab_con" style="display: block;"> The column 1 The content of </div>
<div class = "tab_con"> The column 2 The content of </div>
<div class = "tab_con"> The column 3 The content of </div>
</div>
<script>
var tab_list = document.querySelector(".tab_list").querySelectorAll("li");
var tab_con = document.querySelectorAll(".tab_con");
for(var i = 0;i<tab_list.length;i++){
tab_list[i].onclick = function(){
// The columns above switch
for(var j = 0;j<tab_list.length;j++){
tab_list[j].className = "tab";
}
tab_list[i].className = "tab red";
// The effect of the following text
for(var j = 0;j<tab_con.length;j++){
tab_con[j].style.display = "none";
}
tab_con[i].style.display = "block";
}
}
</script>
</body>
</html>
It turns out that the report is wrong :Uncaught TypeError: Cannot set properties of undefined (setting 'className') at HTMLDivElement.tab.<computed>.onclick
Baidu for a while , Test yourself again , Find the following code :
<script>
var tab_list = document.querySelector(".tab_list").querySelectorAll("li");
var tab_con = document.querySelectorAll(".tab_con");
for(var i = 0;i<tab_list.length;i++){
tab_list[i].onclick = function(){
console.log(" The column " + i + " By clicking the ");
}
}
</script>
Printing ,i by 3, Instead of thinking 0、1、2.
After consulting the data, we know :
stay for In circulation , For each tab_list binding onclick Event monitoring , But when the function executes ,i The cycle has ended , So the printout is 3.
namely , We are for The event listener function in the loop , You need to avoid using circular variables i
that , If it involves tab_list[i], We can use this; If it involves tab_con[i], That is to use i Get other elements , Then we can give tab_list Add a property index, And then in onclick Function , Get this property , That is, get what we want i
The code is as follows :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.tab{
width: 400px;
margin: 100px auto;
}
.tab .tab_list li{
display: inline-block;
width: 100px;
height: 50px;
line-height: 50px;
text-align: center;
background-color: #aaa;
}
.tab .tab_list .current{
background-color: yellow;
}
.content{
display: none;
}
</style>
</head>
<body>
<div class = "tab">
<div class = "tab_list">
<li> The column 1</li>
<li> The column 2</li>
<li> The column 3</li>
</div>
<div class = "tab_con" style="display: block;"> The column 1 The content of </div>
<div class = "tab_con"> The column 2 The content of </div>
<div class = "tab_con"> The column 3 The content of </div>
</div>
<script>
var tab_list = document.querySelector(".tab_list").querySelectorAll("li");
var tab_con = document.querySelectorAll(".tab_con");
for(var i = 0;i<tab_list.length;i++){
tab_list[i].setAttribute("index",i);
tab_list[i].onclick = function(){
var index = this.getAttribute("index");
console.log(" The column " + index + " By clicking the ");
// The columns above switch
for(var j = 0;j<tab_list.length;j++){
tab_list[j].className = "";
}
tab_list[index].className = "current";
console.log(this.className);
// The effect of the following text
for(var j = 0;j<tab_con.length;j++){
tab_con[j].style.display = "none";
}
tab_con[index].style.display = "block";
}
}
</script>
</body>
</html>
边栏推荐
- Yunda's cloud based business in Taiwan construction 𞓜 practical school
- What happens when redis runs out of memory
- Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
- Click to send text messages without response is a common problem for many users in building the elegant grass Dragonfly Q system - solve the problem of clicking to send text messages without response
- John
- Methods for obtaining some information of equipment
- Socket, network model notes
- Gavin's insight on transformer live class - line by line analysis and field experiment analysis of insurance BOT microservice code of insurance industry in the actual combat of Rasa dialogue robot pro
- Soft exam information system project manager_ Management Science (Operations Research) 2--- senior information system project manager of soft test 034
- Mongodb basic concept learning - Documentation
猜你喜欢

Mongodb delete data

Asemi fast recovery diode us1m parameters, us1m recovery time, us1m voltage drop

Guess the size of the number

MySQL uses the where condition to find strange results: solve

Es11 new methods: dynamic import(), bigint, globalthis, optional chain, and null value merging operator
Wind farm visualization: wind farm data

Soft exam information system project manager_ Management Science (Operations Research) 2--- senior information system project manager of soft test 034
[interview with a large factory] meituan had two meetings. Was there a surprise in the end?

Three tier architecture experiment

What elements are indispensable for the development of the character? What are the stages
随机推荐
Wind farm visualization: wind farm data
DF command – displays disk space usage
[Suanli network] technological innovation of Suanli Network -- Key Technologies of green and security
CTFSHOW
[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]
Day22 send request and parameterization using JMeter
[kicad image] download and installation
D compile time reflection
Curl command – file transfer tool
Kotlin reflection -- Notes
HCIP Day 16
Global and China chemical mechanical polishing abrasive materials market demand outlook and investment scale forecast report 2022 Edition
JSON. toJSONString(object, SerializerFeature.WriteMapNullValue); Second parameter action
Tencent and China Mobile continued to buy back with large sums of money, and the leading Hong Kong stocks "led" the market to rebound?
Understanding the dynamic mode of mongodb document
What is the IP address
Notes on dashboard & kuboard installation in kubernetes cluster
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications
Laravel8 fill data
The e-book "action guide for large organizations to further promote zero code application platform" was officially released!