最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery利用鍵盤上下鍵移動表格內(nèi)容

 更新時間:2022年02月22日 10:10:10   作者:Cheang_Bokgaai  
這篇文章主要為大家詳細介紹了jQuery利用鍵盤上下鍵移動表格內(nèi)容,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了jQuery利用鍵盤上下鍵移動表格內(nèi)容的具體代碼,供大家參考,具體內(nèi)容如下

在我們編輯表格內(nèi)容時,經(jīng)常需要將表格內(nèi)容的位置進行移動,而利用鍵盤上下鍵進行移動是十分方便的。

效果如下:

基本原理是:先使用鼠標選中其中的一行,隨后使用鍵盤上下鍵,通過獲取不同的鍵值區(qū)分上移和下移的操作,隨后首先交換兩行的編號,隨后交換兩行的內(nèi)容,保證了兩行內(nèi)容移動而編號不改變。

下面是代碼:

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"); });
? ? ? ? //將當前行變?yōu)榧t色
? ? ? ??
?
? ? ? ? //獲取當前行數(shù)?
}

以上為鼠標的點擊一行的操作,獲取當前的行數(shù),以及將當前行變?yōu)榧t色。

<tr id=\"line"+num+"\" onclick='clickA(this)'></tr>

這個表格每一行的點擊事件綁定。

?$(document).keydown(function(event){ ?
?
? ? ? ? ? if(event.keyCode == 38){ ?
? ? ? ? ? ? //鍵盤上鍵
? ? ? ? ? ? ?up_exchange_line();
? ? ? ? ? }else if (event.keyCode == 40){ ??
? ? ? ? ? ? down_exchange_line();
? ? ? ? ? ? //鍵盤下鍵
? ? ? ? ? }
? ? ? });

這個是獲取撲捉鍵盤上下鍵動作,進行不同的操作

function up_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('請點擊一行');
? ? ? ? ? ? return false;
? ? ? ? ? ? //未點擊,無當前行要求用戶點擊一行
? ? ? ? }
? ? ? ?
? ? ? ? if (nowrow <= 1) {
? ? ? ? ? ? alert('已到達頂端!');
? ? ? ? ? ? return false;
? ? ? ? ? ? //上移到頂端后提示
? ? ? ? }
? ? ? ??
? ? ? ? var up = nowrow - 1;
? ? ? ?//首先交換兩行序號
? ? ? ? $('#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;
? ? ? ? ? ? ?//改變當前行,為繼續(xù)上移做準備
}

這個上移的方法,首先獲取當前行數(shù),隨后獲取上一行的行數(shù),首先進行序號的交換,隨后將當前行的紅色變至上一行,隨后交換所有的內(nèi)容,最后更新當前行。這樣保證了,內(nèi)容和當前所在行會跟這個鍵盤上鍵而移動而序號可以保持不變。

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('請選擇一項!');
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? maximum=$("#table1").find("tr").length ;
? ? ? ? if (nowrow >= maximum-1) {
? ? ? ? ? ? alert('已經(jī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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

武川县| 通渭县| 武强县| 富蕴县| 鞍山市| 阿克陶县| 开封县| 内黄县| 太康县| 鄂尔多斯市| 景洪市| 霍州市| 长垣县| 景德镇市| 东乡| 探索| 汉沽区| 页游| 金寨县| 大新县| 鄂州市| 奉节县| 河曲县| 赤水市| 和龙市| 普陀区| 海口市| 遂昌县| 庆安县| 台中市| 蓝田县| 镇康县| 怀集县| 岳阳县| 淮南市| 察哈| 哈巴河县| 竹溪县| 定南县| 乌兰浩特市| 大连市|