• JS点击按钮获取列表信息,纯JS代码创建表格,如何引入CSS框架,<svg>标签


    
    
    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>title>
    		<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />//引入css框架
    	head>
    	<style type="text/css">
    		.button-trash{//为class="table"的class标签设置css格式
    			display: inline-block;
    			border: 0;
    			width: 2rem;
    			height: 1rem;
    			outline: 0;
    			background-image:url(
    "data:image/svg+xml,"
    			);
    			background-repeat: no-repeat; 
    			background-size: 1rem 1rem;
    			background-color: transparent;
    		}
    		.button-trash:focus {//为class="table"的class标签设置鼠标移入时的css格式
    			border: 0;
    			outline: 0;
    		}
    	style>
    	<body>
    		<table class="table">//表格table
    		  <thead>//表头
    			<tr>//表头内容
    			  <th scope="col">学号th>//具体每列的表头内容
    			  <th scope="col">姓名th>
    			  <th scope="col">班级th>
    			  <th scope="col">性别th>
    			  <th scope="col">操作th>
    			tr>
    		  thead>
    		  <tbody id="tb">//表格主体,用来存放具体数据,其内容用JS代码动态生成
    		  tbody>
    		table>
    		<script type="text/javascript">//JS代码部分
    			window.onload = function(){
    				var students = [//,这是一个JSON对象数组,[]表示数组,其中的每个{}表示一个JSON对象
    				  {
    				    "studentNum": "201711104043",
    				    "name": "马阳",
    				    "class": "软件182",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201804102067",
    				    "name": "夏一鸣",
    				    "class": "软件181",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201810101153",
    				    "name": "李志勇",
    				    "class": "软件182",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201811104001",
    				    "name": "姜霞",
    				    "class": "软件181",
    				    "gender": "女"
    				  },
    				  {
    				    "studentNum": "201811104002",
    				    "name": "齐永智",
    				    "class": "软件181",
    				    "gender": "女"
    				  },
    				  {
    				    "studentNum": "201811104003",
    				    "name": "王雯雯",
    				    "class": "软件181",
    				    "gender": "女"
    				  },
    				  {
    				    "studentNum": "201811104004",
    				    "name": "高小菲",
    				    "class": "软件181",
    				    "gender": "女"
    				  },
    				  {
    				    "studentNum": "201811104005",
    				    "name": "赵蕊蕊",
    				    "class": "软件181",
    				    "gender": "女"
    				  },
    				  {
    				    "studentNum": "201811104006",
    				    "name": "骆家禾",
    				    "class": "软件181",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201811104007",
    				    "name": "王飞",
    				    "class": "软件181",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201811104008",
    				    "name": "倪恒智",
    				    "class": "软件181",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201811104009",
    				    "name": "李泽凯",
    				    "class": "软件181",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201811104010",
    				    "name": "马龙",
    				    "class": "软件181",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201811104011",
    				    "name": "王筝",
    				    "class": "软件181",
    				    "gender": "男"
    				  },
    				  {
    				    "studentNum": "201811104012",
    				    "name": "王梓一",
    				    "class": "软件181",
    				    "gender": "男"
    				  }
    				];
    				var tableRow, cellNum, cellName, cellClass, cellGender, cellOperation, btnDelete;
    				var studentsTbody = document.getElementById("tb");//studentsTbody变量绑定id='tb'的tbody标签
    				for(i = 0; i < students.length; i++) {//遍历JSON对象数组
    					var student = students[i];//每次循环取一个JSON对象
    					tableRow = document.createElement('tr');//创建tr
    					studentsTbody.appendChild(tableRow);//tr加到Tbody中
    					
    					cellNum = document.createElement('td');//创建td
    					cellNum.textContent = student.studentNum;//给td内容赋值
    					tableRow.appendChild(cellNum);//td加到tr中
    					
    					cellName = document.createElement('td');
    					cellName.textContent = student.name;
    					tableRow.appendChild(cellName);
    					
    					cellClass = document.createElement('td');
    					cellClass.textContent = student.class;
    					tableRow.appendChild(cellClass);
    					
    					cellGender = document.createElement('td');
    					cellGender.textContent = student.gender;
    					tableRow.appendChild(cellGender);
    					
    					cellOperation = document.createElement('td');//创建td
    					btnDelete = document.createElement('button');//创建btn
    					btnDelete.className = 'button-trash';//btn属性class赋值
    					cellOperation.appendChild(btnDelete);//btn加到td里
    					tableRow.appendChild(cellOperation);//td加到tr里
    					btnDelete.addEventListener('click', function() {
    						confirm(`确定要删除学生${this.parentElement.parentElement.children[1].innerText}${this.parentElement.parentElement.children[0].innerText})吗?`);//confirm为window的弹窗事件、this代指当前的对象dtnDelete、parentElement表示对象的父标签,即上层标签、children表示对象的孩子标签,可能有许多个,所以返回值是一个含有许多标签的数组,使用[0]、[1]获取数组中具体的某个标签。
    					});
    				}
    			}
    		script>
    	body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
  • 相关阅读:
    IntelliJ IDE 插件开发指南
    使用docker安装MySQL,Redis,Nacos,Consul教程
    C++标准模板(STL)- 输入/输出操纵符-(std::setprecision,std::setw)
    【SQL4天必知必会】day3. 子查询、联表查询
    新能源国标接入随想
    使用Netty进行协议开发:多协议支持与自定义协议的实现
    仅 7 人的公司,年收 100 万美元!但从谷歌辞职的老板决定:以 60 万美元,卖掉...
    华为云大咖说:开发者应用AI大模型的“道、法、术”
    Vue 监听store数据变化
    论文研读-Thread-level transactional memory(TTM)
  • 原文地址:https://blog.csdn.net/m0_53881899/article/details/126689999