当前位置:网站首页>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>
边栏推荐
- Mongodb basic concept learning - Documentation
- Grep command – powerful text search tool
- An easy problem
- Ping command – test network connectivity between hosts
- JSON. toJSONString(object, SerializerFeature.WriteMapNullValue); Second parameter action
- General test point ideas are summarized and shared, which can be directly used in interview and actual software testing
- [interview with a large factory] meituan had two meetings. Was there a surprise in the end?
- [network security] sharing of experience and ideas of an emergency battle
- Getting started with Silverlight development 1
- CST8227
猜你喜欢

MySQL tuning --01--- optimization steps and system performance parameters

@Detailed explanation of valid annotation usage

Mongodb delete data

Methods for obtaining some information of equipment

Invalid bound statement (not found)
Vscode voice notes to enrich information (Part 1)

Tablespace free space

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

How to use asemi FET 7n80 and how to use 7n80

RT thread i/o device model and layering
随机推荐
John
Soft exam information system project manager_ Information system security management - Senior Information System Project Manager of soft test 026
C switch nested syntax
JSON. toJSONString(object, SerializerFeature.WriteMapNullValue); Second parameter action
Explain @builder usage
北京网上开股票账户安全吗?
Highway
[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]
RM command – remove file or directory
What happens when redis runs out of memory
Tail command – view the contents at the end of the file
Getting started with mongodb
Rhcsa day 4
Ping command – test network connectivity between hosts
How to use asemi FET 7n80 and how to use 7n80
Leetcode sword finger offer question brushing - day 27
The e-book "action guide for large organizations to further promote zero code application platform" was officially released!
Asemi fast recovery diode us1m parameters, us1m recovery time, us1m voltage drop
VMware virtual machine prompt: the virtual device ide1:0 cannot be connected because there is no corresponding device on the host.
Cnpm installation