jQuery動(dòng)態(tài)生成表格及右鍵菜單功能示例
本文實(shí)例講述了jQuery動(dòng)態(tài)生成表格及右鍵菜單功能。分享給大家供大家參考,具體如下:
這里用的是 jquery 1.4.1 的庫(kù)文件,具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>無(wú)標(biāo)題頁(yè)</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("數(shù)據(jù)插入成功!"); 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"); }); //綁定鼠標(biāo)移入移出事件到表格的行 $("#tbAdd tr:has(td)") .live("mouseover", function () { $(this).css("cursor", "pointer").css("background", "#bcc7d8"); }) .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("top", 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>編號(hào)</th><th>CPU</th><th>主板</th><th>內(nèi)存</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>內(nèi)存:</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>
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery切換特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jquery中Ajax用法總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery動(dòng)態(tài)生成不規(guī)則表格(前后端)
- JQuery 動(dòng)態(tài)生成Table表格實(shí)例代碼
- jQuery動(dòng)態(tài)生成Bootstrap表格
- JQuery Ajax動(dòng)態(tài)生成Table表格
- Jquery 動(dòng)態(tài)生成表格示例代碼
- jQuery實(shí)現(xiàn)多按鈕單擊變色
- 用JQuery實(shí)現(xiàn)表格隔行變色和突出顯示當(dāng)前行的代碼
- jQuery代碼實(shí)現(xiàn)表格中點(diǎn)擊相應(yīng)行變色功能
- jQuery輕松實(shí)現(xiàn)表格的隔行變色和點(diǎn)擊行變色的實(shí)例代碼
- jQuery插件實(shí)現(xiàn)表格隔行變色及鼠標(biāo)滑過(guò)高亮顯示效果代碼
- jquery實(shí)現(xiàn)表格中點(diǎn)擊相應(yīng)行變色功能效果【實(shí)例代碼】
- jQuery實(shí)現(xiàn)表格隔行及滑動(dòng),點(diǎn)擊時(shí)變色的方法【測(cè)試可用】
- jQuery實(shí)現(xiàn)動(dòng)態(tài)生成表格并為行綁定單擊變色動(dòng)作的方法
相關(guān)文章
jquery實(shí)現(xiàn)疊層3D文字特效代碼分享
本文主要介紹了jquery實(shí)現(xiàn)疊層3D文字特效,實(shí)現(xiàn)很簡(jiǎn)單2015-08-08jQuery插件FusionCharts實(shí)現(xiàn)的MSBar3D圖效果示例【附demo源碼】
這篇文章主要介紹了jQuery插件FusionCharts實(shí)現(xiàn)的MSBar3D圖效果,結(jié)合完整實(shí)例形式分析了jQuery使用FusionCharts載入xml數(shù)據(jù)繪制MSBar3D圖的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03jQuery動(dòng)態(tài)添加<input type="file">
有時(shí)候需要在頁(yè)面上允許用戶上傳多個(gè)文件,個(gè)數(shù)由用戶自己決定,個(gè)數(shù)多了也可以刪除,使用jQuery可以很簡(jiǎn)單的實(shí)現(xiàn)這個(gè)功能2016-04-04jquery導(dǎo)航制件jquery鼠標(biāo)經(jīng)過(guò)變色效果示例
這篇文章主要介紹了jquery鼠標(biāo)經(jīng)過(guò)變色效果代碼,大家參考使用吧2013-12-12jQuery實(shí)現(xiàn)彈出帶遮罩層的居中浮動(dòng)窗口效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)彈出帶遮罩層的居中浮動(dòng)窗口效果,涉及jQuery針對(duì)頁(yè)面元素的運(yùn)算與樣式的動(dòng)態(tài)修改相關(guān)技巧,需要的朋友可以參考下2016-09-09jquery分頁(yè)優(yōu)化操作實(shí)例分析
這篇文章主要介紹了jquery分頁(yè)優(yōu)化操作,在前文jQuery分頁(yè)顯示的基礎(chǔ)上實(shí)現(xiàn)了避免頻繁查詢的相關(guān)優(yōu)化操作技巧,需要的朋友可以參考下2019-08-08jQuery使用ajax跨域獲取數(shù)據(jù)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇jQuery使用ajax跨域獲取數(shù)據(jù)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05