开头及CSS样式代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.11.0.js" ></script>
<style>
*{margin: 0;}
table{
width: 800px;
margin: 0 auto;
border: 1px solid #ddd;
border-collapse: collapse;
text-align: center;
}
td{
height: 40px;
border: 1px solid #ddd;
}
input{
width: 100px;
border: 1px solid #ddd;
outline: none;
padding: 6px;
border-radius: 4px;
display: none;
}
.btn{
border: none;
outline: none;
cursor: pointer;
padding: 6px 8px;
color: #fff;
border-radius: 4px;
background: darkred;
}
.btn-red{
background: darkred;
}
.que_change{
display: none;
margin-right: 7px;
}
.add_btn{
background: red;
}
.hide{
display: none;
}
.show{
display: inline-block;
}
.check{
margin-left: 7px;
}
.model{
position:absolute;
top:0%;left:0%;
display:none;
background:rgba(0,0,0,0.3);
width:100%;
height:100%;
position:fixed;
z-index:9999;
font-family: "微软雅黑";
}
.content{
position: absolute;
left: 35%;
top: 25%;
border-radius: 8px;
width: 30%;
height: 40%;
background-color: #fff;
}
.model_header{
height: 40px;
background: #DDDDDD;
border-radius: 8px 8px 0 0 ;
}
h3{
line-height: 40px;
margin-left: 5px;
}
.setbar{
margin: 30px;
height: 250px;
}
.setbar p{
margin-bottom: 20px;
}
.model_footer{
height: 40px;
background: #DDDDDD;
border-radius: 0 0 8px 8px ;
}
.fr{
float: right;
margin: 7px 10px 0 0 ;
}
</style>
</head>
DOM节点代码
<body>
<table>
<tr>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
成绩
</td>
<td>
操作
</td>
</tr>
</table>
</body>
模态框代码
<div class="model">
<div class="content">
<div class="model_header">
<h3>查看</h3>
</div>
<div class="model_body">
<div class="setbar">
<p>姓名: <span>张三</span></p>
<p>年龄: <span>20</span></p>
<p>分数: <span>80</span></p>
</div>
</div>
<div class="model_footer">
<button class="close_model btn fr">确定</button>
</div>
</div>
</div>
jquery区域
ajax的加载及节点插入
$.ajax({
url:"data/data.json",
type:"get",
success:function(res){
var str = ""
for (var i=0;i<res.length;i++) {
str += '<tr><td><span>'+res[i].name+'</span><input type="text" /></td><td><span>'+res[i].age+'</span><input type="text" /></td><td><span>'+res[i].score+'</span><input type="text" /></td><td style="width: 250px;"><button class="btn btn-red change">修改</button> <button class="btn que_change">确定</button><button class="btn btn-red remove">删除</button><button class="btn btn-red check">查看</button></td></tr>'
}
$("table").append(str)
$("table").append('<tr class="addtr"><td colspan="4" class="add_td"><button class="btn add_btn">增加</button></td></tr>')
}
})
增加
$(document).on("click",".add_btn",function(){
$(".addtr").remove()
$("table").append('
<tr>
<td>
<span class="hide">小明</span>
<input type="text" class="show"/>
</td>
<td>
<span class="hide">20</span><input type="text" class="show"/>
</td>
<td><span class="hide">80</span><input type="text" class="show"/>
</td>
<td>
<button style="margin-right: 7px;" class="btn btn-red change hide">修改
</button>
<button class="btn que_change show">确定</button>
<button class="btn btn-red remove">删除</button>
<button class="btn btn-red check">查看</button>
</td>
</tr>
<tr class="addtr"><td colspan="4" class="add_td"><button class="btn add_btn">增 加</button></td></tr>')
})
删除
$(document).on("click",".add_btn",function(){
$(".addtr").remove()
$("table").append('
<tr>
<td>
<span class="hide">小明</span><input type="text" class="show"/>
</td>
<td>
<span class="hide">20</span><input type="text" class="show"/>
</td>
<td>
<span class="hide">80</span><input type="text" class="show"/></td>
<td>
<button style="margin-right: 7px;" class="btn btn-red change hide">修改</button>
<button class="btn que_change show">确定</button>
<button class="btn btn-red remove">删除</button>
<button class="btn btn-red check">查看</button>
</td>
</tr>
<tr class="addtr">
<td colspan="4" class="add_td"><button class="btn add_btn">增加</button> </td>
</tr>')
})
修改
var arrInfo = []
$(document).on("click",".change",function(){
arrInfo = []
$(this).hide()
$(this).siblings(".que_change").show()
$(this).parent().siblings().find("span").each(function(){
arrInfo.push($(this).text())
})
$(this).parent().siblings().find("input").each(function(i){
$(this).val(arrInfo[i])
})
$(this).parent().siblings().find("span").hide()
$(this).parent().siblings().find("input").show()
})
// 确认修改
var arrList = []
$(document).on("click",".que_change",function(){
arrList = []
$(this).hide()
$(this).siblings(".change").show()
$(this).parent().siblings().find("input").each(function(){
arrList.push($(this).val())
})
$(this).parent().siblings().find("span").each(function(i){
$(this).text(arrList[i])
})
$(this).parent().siblings().find("span").show()
$(this).parent().siblings().find("input").hide()
})
查看
$(document).on("click",".check",function(){
var checkList = []
$(".model").css("display","block")
$(this).parents("tr").find("span").each(function(){
checkList.push($(this).text())
})
$(".model").find("span").each(function(i){
$(this).text(checkList[i])
})
})
$(document).on("click",".close_model",function(){
$(".model").css("display","none")
})