jQuery动态生成表格及右键菜单功能示例
网络编程 2021-07-04 19:20www.168986.cn编程入门
这篇文章主要介绍了jQuery动态生成表格及右键菜单功能,结合实例形式分析了jQuery表格的动态操作及鼠标事件响应相关技巧,需要的朋友可以参考下
本文实例讲述了jQuery动态生成表格及右键菜单功能。分享给大家供大家参考,具体如下
这里用的是 jquery 1.4.1 的库文件,具体代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://.w3./1999/xhtml"> <head> <title>无标题页</title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> var id = 0; function addInfo() { var cpu = document.getElementById("txtCpu"); var zhuban = document.getElementById("txtZhuban"); var neicun = document.getElementById("txtNeicun"); var yingpan = document.getElementById("txtYingpan"); var tb = document.getElementById("tbAdd"); //alert("数据插入成功!"); var tr = tb.insertRow(); var td0 = tr.insertCell(); td0.innerHTML = id; var td1 = tr.insertCell(); td1.innerHTML = cpu.value; var td2 = tr.insertCell(); td2.innerHTML = zhuban.value; var td3 = tr.insertCell(); td3.innerHTML = neicun.value; var td4 = tr.insertCell(); td4.innerHTML = yingpan.value; id++; $("#tbAdd").append(tr); } $(function () { var clickedTrIndex = -1; $("#addForm>input[type=button]") .live("click", function () { $("#tbAdd tr:has(td):even").css("background", "#ebebeb"); }); //绑定鼠标移入移出事件到表格的行 $("#tbAdd tr:has(td)") .live("mouseover", function () { $(this).css("cursor", "pointer").css("background", "#b7d8"); }) .live("mouseleave", function () { var trIndex = $(this).index(); if (trIndex % 2 == 0) { $(this).css("background", "#ebebeb"); } else { $(this).css("background", ""); } }) .live("mousedown", function (event) { if (event.button == 2) { x = event.clientX; y = event.clientY; $("#contextMenu").css("display", "block").css("left", x).css("", y); clickedTrIndex = $(this).index(); } }); $("#contextMenu") .mouseover(function () { $(this).css("cursor", "pointer"); }); $("body") .live("mouseup", function (event) { if (event.button == 0) { $("#contextMenu").css("display", "none"); } }); $("#contextMenu li") .mouseover(function () { $(this).css("background", "#C1D7EE"); }) .mouseout(function () { $(this).css("background", ""); }) .click(function () { var deleteStr = $(this).children("a").attr("title"); if (deleteStr == "delete") { $("#tbAdd tr:has(td):eq(" + clickedTrIndex + ")").remove(); } else { alert("按下了" + deleteStr); } }); }); </script> <style type="text/css"> #tbAdd{ } #tbAdd tr td{ height:30px; text-align:center; } #tbAdd thead tr th{ width:90px; height:30px; text-align:center; } #addForm input[type=text]{ margin-bottom:8px; width:150px; } #contextMenu{ width:150px; padding:5px 0px 5px 5px; line-height:24px; background-color:#F0F0F0; position:absolute; display:none; } #contextMenu ul{ margin:0px; } #contextMenu li{ margin:0px; margin-left:-15px; float:left; width:100%; list-style-type:none; } #contextMenu li a{ text-decoration:none; padding:5px 0px 5px 12px; display:block; color:#282828; } </style> </head> <body onContextmenu="return false;"> <div> <table id="tbAdd" cellpadding="0" cellspacing="0" border="1" style="border-collapse:collapse;"> <thead> <tr> <th>编号</th><th>CPU</th><th>主板</th><th>内存</th><th>硬盘</th> </tr> </thead> </table> <br /> <div id="addForm"> <span>CPU</span><input type="text" id="txtCpu" /><br /> <span>主板</span><input type="text" id="txtZhuban" /><br /> <span>内存</span><input type="text" id="txtNeicun" /><br /> <span>硬盘</span><input type="text" id="txtYingpan" /><br /> <input type="button" value="添加信息" onclick="addInfo()" /><br /> </div> <div id="contextMenu"> <ul> <li><a href="#" title="add">添加信息</a></li> <li><a href="#" title="delete">删除信息</a></li> <li><a href="#" title="modify">修改信息</a></li> <li><a href="#" title="save">保存信息</a></li> </ul> </div> </div> </body> </html>
更多关于jQuery相关内容感兴趣的读者可查看本站专题《》、《》、《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家jQuery程序设计有所帮助。
上一篇:微信小程序 增、删、改、查操作实例详解
下一篇:Vue数据驱动模拟实现5
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指