web前端实现JQuery表格实时编辑增加删除

web前端实现JQuery对表格实时的编辑、增加和删除。

web前端实现JQuery表格实时编辑增加删除-上流阁

代码如下

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
</head>
<body>
<input type="text" name="t1" id="t1" placeholder="请输入rows" style="height: 30px;"/>
<input type="text" name="t2" id="t2" placeholder="请输入cols" style="height: 30px;"/>
<input type="button" name="b1" id="b1" value="生成表格" style="height: 30px;"/>
<div></div>
<script type="text/javascript">

$("div").append("<table></table>");
$("input[name='b1']").click(function(){
$("table").html("").css("borderCollapse","collapse")
var rows=$("#t1").val();
var cols=$("#t2").val();
for(var i=0;i<rows;i++){
$("table").append("<tr></tr>");
}
for(var i=0;i<cols;i++){
$("tr").append("<td></td>");
}
$("td").css({width:"150px",height:"40px",border:" 1px solid black",textAlign:"center"})
$("tr").append("<input type='button' class='c2 c3' name='delete1' value='删除'/>");
$("tr").append("<input type='button' class='c2 c3' name='addtop' value='添加上一行'/>");
$("tr").append("<input type='button' class='c2 c3' name='addbottom' value='添加下一行'/>");
$(".c3").css({width:"150px",height:"42px",background:"#e35435",border:"1px solid black"})
$("input[name='addtop']").click(function(){
var newtr=$(this).parent().clone(true);
newtr.children("td").html("");
$(this).parent().before(newtr);
newtr.css("background","red");
})
$("input[name='addbottom']").click(function(){
var newtr=$(this).parent().clone(true);
$(this).parent().after(newtr);
newtr.children("td").html("");
newtr.css("background","gold");
})
$("input[name='delete1']").click(function(){
$(this).parent().detach();
})
$("td").click(function(e){
var that=this;
var content=$(that).text();
$(that).html("<input type='text' class='in1' />");
$("input[class='in1']").css({width:"150px",height:"38px",border:"0",textAlign:"center"})
$("input[class='in1']").focus();
$("input[class='in1']").val(content);
$("input[class='in1']").click(function(e){
e.stopPropagation();
})
$("input[class='in1']").blur(function(){
var content=$(this).val();
$(that).text(content);
})
})
})
</script>
</body>
</html>

*文章为作者独立观点,不代表上流阁立场
本文由 江风成 授权 上流阁 发表,并经上流阁编辑。转载此文章须经作者同意,并请附上出处(上流阁)及本页链接。原文链接https://www.o6c.com/web/2016/10/27/469.html
发表评论

坐等沙发
相关文章
WEB前端Nodejs升级高版本启动错误ERESOLVE unable to resolve dependency tree
WEB前端Nodejs升级高版本启动错误ERESOL…
GoAccess轻量nginx日志分析工具与中文可视化goaccess-1.6.2.tar
GoAccess轻量nginx日志分析工具与中文可…
HTML中Data URI scheme BASE64 文件的拼接头
HTML中Data URI scheme BASE64 文件的拼…
VUE el-input__suffix样式大小修改
VUE el-input__suffix样式大小修改
VUE el ui el-date-picker时间禁用范围禁用干货
VUE el ui el-date-picker时间禁用范围…
VUE 通过URL下载修改文件名的方法 和原生js 下载
VUE 通过URL下载修改文件名的方法 和原…
javaweb开发程序员php开发,微信开发。接受定制开发

最新评论