赞
踩
<html> <head> <meta charset="UTF-8"> <title>学生信息表</title> <style> </style> <script> </script> </head> <body> <div> <table> <tr> <td>姓名</td> <td>年龄</td> <td>学号</td> </tr> </table> </div> </body> </html>
<style>
table{
width: 100%;
border: 1px;
border-style: solid;
}
table td{
border: 0.5px;
border-style: solid;
}
</style>
1.首先在HTML中插入button,点击按钮时进行新增表格
<button onclick="new();">新增</button>
2.接着,编写new()函数体
<script> function add(){ // 获取table标签元素 let table = document.getElementById("work_table"); // 创建新行 let newRow = table.insertRow(); // 创建三个新单元格 let cellName = newRow.insertCell(); let cellAge = newRow.insertCell(); let cellID = newRow.insertCell(); // 向表格中插入元素 cellName.innerHTML = "张三"; cellAge.innerHTML = "男"; cellID.innerHTML = "1"; } </script>
3.运行效果
上篇文章链接 link.
1.实现弹出窗口,这里和上篇一样,不详细说明
<html> <head> <meta charset="UTF-8"> <title>学生信息表</title> <style> table{ width: 100%; border: 1px; border-style: solid; } table td{ border: 0.5px; border-style: solid; } body{ background-color: cyan; } #popDiv{ display: none; background-color: whitesmoke; z-index: 11; width: 600px; height: 600px; position:fixed; top:0; right:0; left:0; bottom:0; margin:auto; } /* 关闭按钮样式 */ #popDiv .close a { text-decoration: none; color: whitesmoke; } /* 弹出界面的关闭按钮 */ #popDiv .close{ text-align: right; margin-right: 5px; background-color:#2D2C3B; } #popDiv p{ text-align: center; font-size: 25px; font-weight: bold; } </style> <script> function popDiv(){ // 获取div元素 var popBox = document.getElementById("popDiv"); var popLayer = document.getElementById("popLayer"); // 控制两个div的显示与隐藏 popBox.style.display = "block"; popLayer.style.display = "block"; } function closePop(){ // 获取弹出窗口元素 let popDiv = document.getElementById("popDiv"); popDiv.style.display = "none"; } function add(){ // 获取table标签元素 let table = document.getElementById("work_table"); // 创建新行 let newRow = table.insertRow(); // 创建三个新单元格 let cellName = newRow.insertCell(); let cellAge = newRow.insertCell(); let cellID = newRow.insertCell(); // 向表格中插入元素 cellName.innerHTML = "张三"; cellAge.innerHTML = "男"; cellID.innerHTML = "1"; } </script> </head> <body> <button onclick="popDiv();">弹窗</button> <div id="popLayer"> <table id="work_table"> <tr> <td>姓名</td> <td>年龄</td> <td>学号</td> </tr> </table> </div> <div id="popDiv"> <div class="close"> <a href="javascript:void(0)" onclick="closePop()">关闭</a> </div> <p>此处为弹出窗口</p> </div> </body> </html>
2.给弹出窗口设置三个文本框用于输入
<div id="popDiv"> <div class="close"> <a href="javascript:void(0)" onclick="closePop()">关闭</a> </div> <div> 请输入<br/><br/> <form <label>姓名(*)</label> <input type="text" id="NAME"><br/> <label>年龄(*)</label> <input type="text" id="AGE"><br/> <label>工人性别(*)</label> <input type="text" id="SEX"><br/> </form> </div> </div>
3.现在只需要修改add()函数就可以,也就是获取文本框的内容
function add(){ // 获取table标签元素 let table = document.getElementById("work_table"); // 创建新行 let newRow = table.insertRow(); // 创建三个新单元格 let cellName = newRow.insertCell(); let cellAge = newRow.insertCell(); let cellID = newRow.insertCell(); // 获取文本框内容 let name = document.getElementById("NAME"); let sex = document.getElementById("SEX"); let age = document.getElementById("AGE"); // 向表格中插入元素 cellName.innerHTML = name.value; cellAge.innerHTML = age.value; cellSex.innerHTML = sex.value; console.log(typeof(name.value)); closePop(); }
<html> <head> <meta charset="UTF-8"> <title>学生信息表</title> <style> table{ width: 100%; border: 1px; border-style: solid; } table td{ border: 0.5px; border-style: solid; } body{ background-color: cyan; } #popDiv{ display: none; background-color: whitesmoke; z-index: 11; width: 600px; height: 600px; position:fixed; top:0; right:0; left:0; bottom:0; margin:auto; } /* 关闭按钮样式 */ #popDiv .close a { text-decoration: none; color: whitesmoke; } /* 弹出界面的关闭按钮 */ #popDiv .close{ text-align: right; margin-right: 5px; background-color:#2D2C3B; } #popDiv p{ text-align: center; font-size: 25px; font-weight: bold; } </style> <script> function popDiv(){ // 获取div元素 var popBox = document.getElementById("popDiv"); var popLayer = document.getElementById("popLayer"); // 控制两个div的显示与隐藏 popBox.style.display = "block"; popLayer.style.display = "block"; } function closePop(){ // 获取弹出窗口元素 let popDiv = document.getElementById("popDiv"); popDiv.style.display = "none"; } function add(){ // 获取table标签元素 let table = document.getElementById("work_table"); // 创建新行 let newRow = table.insertRow(); // 创建三个新单元格 let cellName = newRow.insertCell(); let cellAge = newRow.insertCell(); let cellID = newRow.insertCell(); // 获取文本框内容 let name = document.getElementById("NAME"); let sex = document.getElementById("SEX"); let ID = document.getElementById("AGE"); // 向表格中插入元素 cellName.innerHTML = name.value; cellAge.innerHTML = age.value; cellSex.innerHTML = sex.value; closePop(); } </script> </head> <body> <button onclick="popDiv();">弹窗</button> <div id="popLayer"> <table id="work_table"> <tr> <td>姓名</td> <td>年龄</td> <td>学号</td> </tr> </table> </div> <div id="popDiv"> <div class="close"> <a href="javascript:void(0)" onclick="closePop()">关闭</a> </div> <div> 请输入<br/><br/> <form <label>姓名(*)</label> <input type="text" id="NAME"><br/> <label>年龄(*)</label> <input type="text" id="AGE"><br/> <label>工人性别(*)</label> <input type="text" id="SEX"><br/> </form> <button onclick="add();">√保存</button> </div> </div> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。