jQuery利用鍵盤上下鍵移動(dòng)表格內(nèi)容
本文實(shí)例為大家分享了jQuery利用鍵盤上下鍵移動(dòng)表格內(nèi)容的具體代碼,供大家參考,具體內(nèi)容如下
在我們編輯表格內(nèi)容時(shí),經(jīng)常需要將表格內(nèi)容的位置進(jìn)行移動(dòng),而利用鍵盤上下鍵進(jìn)行移動(dòng)是十分方便的。
效果如下:



基本原理是:先使用鼠標(biāo)選中其中的一行,隨后使用鍵盤上下鍵,通過獲取不同的鍵值區(qū)分上移和下移的操作,隨后首先交換兩行的編號(hào),隨后交換兩行的內(nèi)容,保證了兩行內(nèi)容移動(dòng)而編號(hào)不改變。
下面是代碼:
function clickA(obj){
? ?
? ? ? ? currentLine=$.trim($(obj).find("td:first-child").html());
? ? ??
? ? ? ? $('#table1 tr').each(function () { $(this).css("background-color", "white"); });
? ? ? ? //將所有行變?yōu)榘咨?
? ? ? ? $('#line' + currentLine).each(function () { $(this).css("background-color", "red"); });
? ? ? ? //將當(dāng)前行變?yōu)榧t色
? ? ? ??
?
? ? ? ? //獲取當(dāng)前行數(shù)?
}以上為鼠標(biāo)的點(diǎn)擊一行的操作,獲取當(dāng)前的行數(shù),以及將當(dāng)前行變?yōu)榧t色。
<tr id=\"line"+num+"\" onclick='clickA(this)'></tr>
這個(gè)表格每一行的點(diǎn)擊事件綁定。
?$(document).keydown(function(event){ ?
?
? ? ? ? ? if(event.keyCode == 38){ ?
? ? ? ? ? ? //鍵盤上鍵
? ? ? ? ? ? ?up_exchange_line();
? ? ? ? ? }else if (event.keyCode == 40){ ??
? ? ? ? ? ? down_exchange_line();
? ? ? ? ? ? //鍵盤下鍵
? ? ? ? ? }
? ? ? });這個(gè)是獲取撲捉鍵盤上下鍵動(dòng)作,進(jìn)行不同的操作
function up_exchange_line(index) {
? ? ? ? if(currentLine != null && currentLine!= " "){
? ? ? ? ? ? nowrow = currentLine;
? ? ? ? ? ? //獲取當(dāng)前行
? ? ? ? }else if (index != null) {
? ? ? ? ? ? nowrow = $.trim($(index).parent().parent().find("td:first-child").html());
? ? ? ? }
? ? ? ? if (nowrow == 0) {
? ? ? ? ? ? alert('請點(diǎn)擊一行');
? ? ? ? ? ? return false;
? ? ? ? ? ? //未點(diǎn)擊,無當(dāng)前行要求用戶點(diǎn)擊一行
? ? ? ? }
? ? ? ?
? ? ? ? if (nowrow <= 1) {
? ? ? ? ? ? alert('已到達(dá)頂端!');
? ? ? ? ? ? return false;
? ? ? ? ? ? //上移到頂端后提示
? ? ? ? }
? ? ? ??
? ? ? ? var up = nowrow - 1;
? ? ? ?//首先交換兩行序號(hào)
? ? ? ? $('#line' + up + " td:first-child").html(nowrow);
? ? ? ? $('#line' + nowrow + " td:first-child").html(up);
? ? ? ? //變色
? ? ? ? $('#table1 tr').each(function () { $(this).css("background-color", "white"); });
? ? ? ? $('#line' + up).css("background-color", "red"); ;
? ? ? ? //獲取兩行的內(nèi)容
? ? ? ? var upContent = $('#line' + up).html();
? ? ? ? var currentContent = $('#line' + nowrow).html();
? ? ? ?//交換內(nèi)容
? ? ? ? $('#line' + up).html(currentContent);
? ? ? ? $('#line' + nowrow).html(upContent);
? ? ? ??
? ? ? ? ? ? ?currentLine = up;
? ? ? ? ? ? ?//改變當(dāng)前行,為繼續(xù)上移做準(zhǔn)備
}這個(gè)上移的方法,首先獲取當(dāng)前行數(shù),隨后獲取上一行的行數(shù),首先進(jìn)行序號(hào)的交換,隨后將當(dāng)前行的紅色變至上一行,隨后交換所有的內(nèi)容,最后更新當(dāng)前行。這樣保證了,內(nèi)容和當(dāng)前所在行會(huì)跟這個(gè)鍵盤上鍵而移動(dòng)而序號(hào)可以保持不變。
function down_exchange_line(index) {
? ? ? ? if(currentLine != null && currentLine != " "){
? ? ? ? ? ? nowrow = currentLine;
? ? ? ? }else if (index != null) {
? ? ? ? ? ? nowrow = $.trim($(index).parent().parent().find("td:first-child").html());
? ? ? ? }
? ? ? ? if (nowrow == 0) {
? ? ? ? ? ? alert('請選擇一項(xiàng)!');
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? maximum=$("#table1").find("tr").length ;
? ? ? ? if (nowrow >= maximum-1) {
? ? ? ? ? ? alert('已經(jīng)是最后一項(xiàng)了!');
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? var dS = parseInt(nowrow) + 1;
? ? ? ? $('#line' + dS + " td:first-child").html(nowrow);
? ? ? ? $('#line' + nowrow + " td:first-child").html(dS);
? ? ? ? //變色
? ? ? ? $('#table1 tr').each(function () { $(this).css("background-color", "white"); });
? ? ? ? $('#line' + dS).css("background-color", "red");?
? ? ? ? //獲取兩行的內(nèi)容
? ? ? ? var nextContent = $('#line' + dS).html();
? ? ? ? var currentContent = $('#line' + nowrow).html();
? ? ? ? //交換內(nèi)容
? ? ? ? $('#line' + dS).html(currentContent);
? ? ? ? $('#line' + nowrow).html(nextContent);
? ? ??
? ? ? ? if(dS>maximum-1){
? ? ? ? ? ? currentLine=dS-1;
? ? ? ? }else{
? ? ? ? ? ? ?currentLine = dS;
? ? ? ? }
? ? ? ??
}同理,下降也是使用相同的方法,只不過是向下交換數(shù)據(jù)。
這樣基于jQuery使用鍵盤上下鍵交換表格內(nèi)容的操作就完成了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery對話框插件ArtDialog在雙擊遮罩層時(shí)出現(xiàn)關(guān)閉現(xiàn)象的解決方法
這篇文章主要介紹了jQuery對話框插件ArtDialog在雙擊遮罩層時(shí)出現(xiàn)關(guān)閉現(xiàn)象的解決方法,涉及針對插件源碼的修改,需要的朋友可以參考下2016-08-08
Eclipse配置Javascript開發(fā)環(huán)境圖文教程
這篇文章主要介紹了Eclipse配置Javascript開發(fā)環(huán)境圖文教程,需要的朋友可以參考下2015-01-01
jQuery序列化form表單數(shù)據(jù)為JSON對象的實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery序列化form表單數(shù)據(jù)為JSON對象的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
點(diǎn)擊表單提交時(shí)出現(xiàn)jQuery沒有權(quán)限的解決方法
擊表單提交的時(shí)候會(huì)出現(xiàn) jQuery 沒有權(quán)限,試了一下jquery自帶的json方式提交成功2014-07-07
jQuery Ajax和getJSON獲取后臺(tái)普通json數(shù)據(jù)和層級json數(shù)據(jù)用法分析
這篇文章主要介紹了jQuery Ajax和getJSON獲取后臺(tái)普通json數(shù)據(jù)和層級json數(shù)據(jù)用法,結(jié)合實(shí)例形式分析了jQuery基于ajax操作json格式數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2016-06-06
jQuery表單校驗(yàn)插件validator使用方法詳解
這篇文章主要為大家詳細(xì)介紹了jQuery表單校驗(yàn)插件validator的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
jQuery實(shí)現(xiàn)動(dòng)態(tài)添加和刪除input框代碼實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)動(dòng)態(tài)添加和刪除input框,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
jquery注冊文本框獲取焦點(diǎn)清空,失去焦點(diǎn)賦值的簡單實(shí)例
下面小編就為大家?guī)硪黄猨query注冊文本框獲取焦點(diǎn)清空,失去焦點(diǎn)賦值的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09

