新聞中心
jQuery實現(xiàn)的可編輯表格完整實例
本文實例講述了jQuery實現(xiàn)的可編輯表格。分享給大家供大家參考,具體如下:
專業(yè)從事網(wǎng)站設(shè)計、成都做網(wǎng)站,高端網(wǎng)站制作設(shè)計,成都微信小程序,網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團隊竭力真誠服務,采用H5頁面制作+CSS3前端渲染技術(shù),響應式網(wǎng)站,讓網(wǎng)站在手機、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項小組,與您實時在線互動,隨時提供解決方案,暢聊想法和感受。
!DOCTYPE
HTML
PUBLIC
"-//W3C//DTD
HTML
4.01//EN"
""
html
head
meta
http-equiv="Content-Type"
content="text/html;
charset=utf-8"
title可編輯表格/title
script
type
=
"text/javascript"
src="jquery-1.7.2.min.js"/script
style
type
=
"text/css"
body{
background:#c0c0c0;
}
#tab{
border-collapse:collapse;
}
#tab
td{
width:50px;
height:18px;
border:1px
solid;
text-align:center;
}
/style
script
type
=
"text/javascript"
$(function(){
var
tds
=
$("#tab
tr
td");
editeTable(tds);
});
function
editeTable(tds){
tds.click(function(){
var
td=$(this);
var
oldText=td.text();
var
input=$("input
type='text'
value='"+oldText+"'/");
td.html(input);
input.click(function(){
return
false;
});
input.css("border-width","1px");
input.css("font-size","12px");
input.css("text-align","center");
input.css("width","0px");
input.width(td.width());
input.trigger("focus").trigger("select");
input.blur(function(){
td.html(oldText);
});
input.keyup(function(event){
var
keyEvent=event
||
window.event;
var
key=keyEvent.keyCode;
var
input_blur=$(this);
switch(key)
{
case
13:
var
newText=input_blur.val();
td.html(newText);
changeCurrConAttrByTable(currTableId);
break;
case
27://按下esc鍵,取消修改,把文本框變成文本
td.html(oldText);
break;
}
});
});
};
/script
/head
body
table
id
=
"tab"
tr
td1/tdtd1/tdtd1/tdtd1/tdtd1/td
/tr
tr
td1/tdtd1/tdtd1/tdtd1/tdtd1/td
/tr
tr
td1/tdtd1/tdtd1/tdtd1/tdtd1/td
/tr
tr
td1/tdtd1/tdtd1/tdtd1/tdtd1/td
/tr
tr
td1/tdtd1/tdtd1/tdtd1/tdtd1/td
/tr
/table
/body
/html
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jquery中Ajax用法總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
用jquery怎么獲取表格中的一行內(nèi)容
一般都會在后臺獲得,比如這個表格的數(shù)據(jù)是用$list這個變量存儲的,$list中每個元素都有id date title file四個字段,checkbox的name是id,現(xiàn)在寫一個js方法用來將選中的復選框傳到后臺,需要使用一個按鈕觸發(fā)下面的方法
function fun(){
var arr = new Array();
$(":checked").each(function(){
arr[] = $(this).attr("name");
});
//然后用任意方法將arr傳到后臺
}
在后臺根據(jù)傳回來的id在$list中獲得要你想要的數(shù)據(jù)。
上面是我覺得最簡單的方法。
下面還有一個在頁面里獲得你要的數(shù)據(jù)的方法
將每一行數(shù)據(jù)都寫在同一個div里,根據(jù)復選框的標示獲得對應div內(nèi)的數(shù)據(jù)
div name=“id”數(shù)據(jù), (最后一列)input type="checkbox" name="id" //div
script
function fun(){
var arr = new Array();
var name = '';
$(":checked").each(function(){
name = $(this).attr("name");
arr[] = $("div[name=" + name + "]").html();
});
}
/script
如何用jquery改變表格單元格的內(nèi)容
var td=$("#你這個單元格的id");//當然了,這只是id選擇器去獲取標簽對象,還有其他很多選擇器!
$(td).html("你要改變的內(nèi)容");
jQuery怎么獲取某表格中的一列td的值?
需要準備的材料分別有:電腦、html編輯器、瀏覽器。
1、首先,打開html編輯器,新建html文件,例如:index.html,并引入jquery,編寫問題基礎(chǔ)代碼。
2、在index.html中的script標簽,輸入jquery代碼:
$('body').append($('tr').find('td:eq(0)').text());
3、瀏覽器運行index.html頁面,此時成功取到了表格的第一頁數(shù)據(jù)并打印。
jquery中如何獲取表格的值
提供你2中思路,具體代碼就不寫了
生成表的時候給修改鏈接一個id,id保存當前行的主鍵比如id="tid"+主鍵,為它添加onclick方法,
var id=this.id.replace("tid",""); 獲取到主鍵
把主鍵拿到數(shù)據(jù)庫中查到相應的數(shù)據(jù)行,賦值給表單,完成!
var parents=$(this).parent().parent();? 獲得tabel行
$(parents).find("td").each(function(){
根據(jù)列的順序賦值給相應的表單元素
})
這個方法有局限性,數(shù)據(jù)是從當前頁面中取得并不是實時讀取數(shù)據(jù)庫,如果數(shù)據(jù)在他處有更改是取不到最新的數(shù)據(jù)的。
jQuery操作table表格
一、數(shù)據(jù)準備
二、操作
//1.鼠標移動行變色
$("#table1 tr").hover(function(){
$(this).children("td").addClass("hover")
},function(){
$(this).children("td").removeClass("hover")
})
$("#table2 tr:gt(0)").hover(function() {
$(this).children("td").addClass("hover");
}, function() {
$(this).children("td").removeClass("hover");
});
//2.奇偶行不同顏色
$("#table3 tbody tr:odd").css("background-color", "#bbf");
$("#table3 tbody tr:even").css("background-color","#ffc");
$("#table3 tbody tr:odd").addClass("odd")
$("#table3 tbody tr:even").addClass("even")
//3.隱藏一行
$("#table3 tbody tr:eq(3)").hide();
//4.隱藏一列
$("#table5 tr td::nth-child(3)").hide();
$("#table5 tr").each(function(){$("td:eq(3)",this).hide()});
//5.刪除一行
// 刪除除第一行外的所有行
$("#table6 tr:not(:first)").remove();
//6.刪除一列
// 刪除除第一列外的所有列
$("#table6 tr td:not(:nth-child(1))").remove();
//7.得到(設(shè)置)某個單元格的值
//設(shè)置table7,第2個tr的第一個td的值。
$("#table7 tr:eq(1) td:nth-child(1)").html("value");
//獲取table7,第2個tr的第一個td的值。
$("#table7 tr:eq(1) td:nth-child(1)").html();
//8.插入一行:
//在第二個tr后插入一行
$("插入3插入插入插入").insertAfter($("#table7 tr:eq(1)"));
//刪除指定行(第二行) $("#table3 tr:gt(0):eq(1)").remove();
(2)刪除列,比如刪除表格中的第二列:
//eq:獲取子元素索引從 0 開始,先刪除表頭$("#table3 tr th:eq(1)").remove();//nth-child:獲取子元素從 1 開始$("#table3 tr td:nth-child(2)").remove();
(3)刪除其它行,比如第二行之外的所有行:
$("#table3 tr:gt(0):not(:eq(1))").remove();
(4)刪除其它列,比如第二列之外的所有列:
//先刪除表頭$("#table3 tr th:not(:eq(1))").remove();$("#table3 tr td:not(:nth-child(2))").remove();
(5)隱藏行,比如隱藏第二行:
$("#table3 tr:gt(0):eq(1)").hide();//或者//$("#table3 tr:gt(0):eq(1)").css("display", "none")//顯示//$("#table3 tr:gt(0):eq(1)").css("display", "");
(6)隱藏列,比如隱藏第二列:
$("#table3 tr th:eq(1)").hide();
$("#table3 tr td:nth-child(2)").hide();
//或者
//$("#table3 tr th:eq(1)").css("display", "none");
//$("#table3 tr td:nth-child(2)").css("display", "none");
//顯示
//$("#table3 tr th:eq(1)").css("display", "");
//$("#table3 tr td:nth-child(2)").css("display", "");
jquery 獲取 table 總行數(shù):
$("table tr").size();
var hang = $("#g").find("tr").length;
jquery 獲取 table 總列數(shù):
$("table td").size();
var lie = $("#g").find("tr").find("td").length-1;
標題名稱:jquery表格內(nèi)容,jq生成表格
標題鏈接:http://www.dlmjj.cn/article/dsipddc.html