qq_肤白貌美大长腿_0
2019-10-30 14:33
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> html, body, div, table, tr, th, td, input { margin: 0; padding: 0; } a { text-decoration: none; color: #ff7474; font-size: 14px; display: block; width: 60px; margin: 0 auto; } button { width: 160px; line-height: 40px; text-align: center; margin: 0 auto; display: block; background-color: #2196F3; color: #fff; border: none; border-radius: 3px; outline: none; } table { width: 600px; margin: 50px auto; table-layout: fixed; border-collapse: collapse; text-align: center; line-height: 40px; border: 1px solid #e2e2e2; } tr:nth-child(2n+1) { background-color: #f7f7f7; } table tr:first-child{ background-color: #f7f7f7!important; } table .active{ background-color: #e0f1ff; } th, td { border: none; } th { color: #333; font-size: 14px; } td { color: #666; font-size: 14px; } </style> </head> <body> <table border="1" width="50%" id="table"> <tr> <th>学号</th> <th>姓名</th> <th>操作</th> </tr> <tr> <td>xh001</td> <td>王小明</td> <td class="del"><a href="javascript:;" onclick="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh002</td> <td>刘小芳</td> <td class="del"><a href="javascript:;" onclick="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh003</td> <td>张三</td> <td class="del"><a href="javascript:;" onclick="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh004</td> <td>李四</td> <td class="del"><a href="javascript:;" onclick="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh005</td> <td>王五</td> <td class="del"><a href="javascript:;" onclick="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh006</td> <td>赵柳</td> <td class="del"><a href="javascript:;" onclick="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> </table> <button type="button" id="btn" onclick="add()">添加一行</button><!--在添加按钮上添加点击事件--> </body> <script> let table = document.querySelector('#table'); function add() { let node = document.createElement('tr'); table.childNodes[1].appendChild(node); let num= parseInt( Math.random() * 10 ); let txt = getTxt(num); node.innerHTML = '<td>xh00'+num+'</td><td>江'+ txt +'</td><td class="del"><a href="javascript:;" onclick="del(this)">删除</a></td>'; mouse() } function getTxt(num) { let txt = ''; switch (num) { case 0 : txt = '灵'; break; case 1 : txt = '梦'; break; case 2 : txt = '叶'; break; case 3 : txt = '月'; break; case 4 : txt = '晚'; break; case 5 : txt = '晴'; break; case 6 : txt = '年'; break; case 7 : txt = '吟'; break; case 8 : txt = '言'; break; case 9 : txt = '烟'; break; } return txt } function del(that) { table.childNodes[1].removeChild(that.parentNode.parentNode) } function mouse() { let tr = table.querySelectorAll('tr'); for(let i=0;i<tr.length;i++){ tr[i].onmouseover = function () { this.classList.add('active'); }; tr[i].onmouseout = function () { this.classList.remove('active'); } } } mouse() </script> </html>
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<script type="text/javascript">
/*window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var aTr=document.getElementsByTagName('tr');
for(var i=0;i<aTr.length;i++)
{
aTr[i].onmouseover=function()
{
this.style.backgroundColor="red";
}
aTr[i].onmouseout=function()
{
this.style.backgroundColor="#fff";
}
}
}*/
window.onload = function(){
var tr=document.getElementsByTagName("tr");
for(var i= 0;i<tr.length;i++)
{
bgcChange(tr[i]);
}
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
}
function bgcChange(obj)
{
obj.onmouseover=function(){
obj.style.backgroundColor="red";
}
obj.onmouseout=function(){
obj.style.backgroundColor="#fff";
}
}
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;
function addTo(){
var tr=document.createElement("tr");
var xh=document.createElement("td");
var xm=document.createElement("td");
var stuno=document.getElementById('stuno');
var name=document.getElementById('name');
if(name.value==""||stuno.value=="")
{
alert("学号或姓名为空!请重新输入");
return;
}
xh.innerHTML=stuno.value;
xm.innerHTML=name.value;
var del=document.createElement("td");
del.innerHTML="<a href='javascript:;' onclick='del(this)' >删除</a>";
var tab=document.getElementById("table");
tab.appendChild(tr);
tr.appendChild(xh);
tr.appendChild(xm);
tr.appendChild(del);
stuno.value=null;
name.value=null;
bgcChange(tr);
}
// 创建删除函数
function del(obj)
{
var tr=obj.parentNode.parentNode;
tr.parentNode.removeChild(tr);
}
</script>
</head>
<body>
<table border="1" width="50%" id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;" onClick="del(this)" >删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" onClick="del(this)">删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
<input type="text" id="stuno" placeholder="输入学生学号">
<input type="text" id="name" placeholder="输入学生姓名">
<input type="button" value="添加" onClick="addTo()" /> <!--在添加按钮上添加点击事件 -->
</body>
</html>
JavaScript进阶篇
468060 学习 · 21891 问题
相似问题