js實現(xiàn)表格的隔行變色和上下移動
本文實例為大家分享了js實現(xiàn)表格的隔行變色和上下移動的具體代碼,供大家參考,具體內(nèi)容如下
話不多說,先看效果圖:
點擊上移或下移
table樣式:
<style> ? ? ? ? table{ ? ? ? ? ? ? border:1px solid greenyellow; ? ? ? ? ? ? width: 300px; ? ? ? ? } ? ? ? ? .hero{ ? ? ? ? ? ? display: none; ? ? ? ? } ? ? ? ? .show{ ? ? ? ? ? ? display: block; ? ? ? ? } </style>
表格代碼:
<body> ? ? <h3>三國英雄人物排行榜</h3> ? ? <input type="button" value="我來添加英雄" id="add1"> ? ? <div class="hero"> ? ? ? ? 英雄:<input type="text" id="heroName"> ? ? </div> ? ? <table id="tab"> ? ? ? ? <tr> ? ? ? ? ? ? <td>排名</td> ? ? ? ? ? ? <td>人物</td> ? ? ? ? ? ? <td> ? ? ? ? ? ? ? ? 操作 ? ? ? ? ? ? </td> ? ? ? ? </tr> ? ? ? ? <tr> ? ? ? ? ? ? <td>1</td> ? ? ? ? ? ? <td>關(guān)羽</td> ? ? ? ? ? ? <td> ? ? ? ? ? ? ? ? <input type="button" onclick="up(this)" value="上移"/><br/> ? ? ? ? ? ? ? ? <input type="button" value="下移" onclick="down(this)"> ? ? ? ? ? ? </td> ? ? ? ? </tr> ? ? ? ? <tr> ? ? ? ? ? ? <td>2</td> ? ? ? ? ? ? <td>馬超</td> ? ? ? ? ? ? <td> ? ? ? ? ? ? ? ? <input type="button" onclick="up(this)" value="上移"/><br/> ? ? ? ? ? ? ? ? <input type="button" value="下移" onclick="down(this)"> ? ? ? ? ? ? </td> ? ? ? ? </tr> ? ? ? ? <tr> ? ? ? ? ? ? <td>3</td> ? ? ? ? ? ? <td>呂布</td> ? ? ? ? ? ? <td> ? ? ? ? ? ? ? ? <input type="button" onclick="up(this)" value="上移"/><br/> ? ? ? ? ? ? ? ? <input type="button" value="下移" onclick="down(this)"> ? ? ? ? ? ? </td> ? ? ? ? </tr> ? ? ? ? <tr> ? ? ? ? ? ? <td>4</td> ? ? ? ? ? ? <td>典韋</td> ? ? ? ? ? ? <td> ? ? ? ? ? ? ? ? <input type="button" onclick="up(this)" value="上移"/><br/> ? ? ? ? ? ? ? ? <input type="button" value="下移" onclick="down(this)"> ? ? ? ? ? ? </td> ? ? ? ? </tr> ? ? ? ? <tr> ? ? ? ? ? ? <td>5</td> ? ? ? ? ? ? <td>張飛</td> ? ? ? ? ? ? <td> ? ? ? ? ? ? ? ? <input type="button" onclick="up(this)" value="上移"/><br/> ? ? ? ? ? ? ? ? <input type="button" value="下移" onclick="down(this)"> ? ? ? ? ? ? </td> ? ? ? ? </tr> ? ? ? ? <tr> ? ? ? ? ? ? <td>6</td> ? ? ? ? ? ? <td>趙云</td> ? ? ? ? ? ? <td> ? ? ? ? ? ? ? ? <input type="button" onclick="up(this)" value="上移"/><br/> ? ? ? ? ? ? ? ? <input type="button" value="下移" onclick="down(this)"> ? ? ? ? ? ? </td> ? ? ? ? </tr> ? ? </table> </body>
JQuery代碼
?//隔行變色 ? ? ?//這里如果感覺麻煩就封裝進(jìn)一個方法里 ? ? $('tr:even').children().css('background-color','#f4fe56') ? ? $('tr:odd').children().css('background-color','#fe9d88') ? ? //顯示輸入框 ? ? $('#add1').click(function () { ? ? ? ? $('.hero').toggleClass('show') ? ? }) ? ? //添加事件,添加英雄 ? ? $('#heroName').blur(function () { ? ? ? ? let val = $(this).val().trim();//文本框輸入的內(nèi)容去除空格 ? ? ? ? let length = $('tr').length; ? ?//獲取tr下的長度,即是,每個tr下td里面的序號 ? ? ? ? let name='<tr>\n' + ? ? ? ? ? ? ' ? ? ? ? ? ?<td>'+length+'</td>\n' + ? ? ? ? ? ? ' ? ? ? ? ? ?<td>'+val+'</td>\n' + ? ? ? ? ? ? ' ? ? ? ? ? ?<td>\n' + ? ? ? ? ? ? ' ? ? ? ? ? ? ? ?<input type="button" οnclick="up(this)" value="上移"/><br/>\n' + ? ? ? ? ? ? ' ? ? ? ? ? ? ? ?<input type="button" value="下移" οnclick="down(this)">\n' + ? ? ? ? ? ? ' ? ? ? ? ? ?</td>\n' + ? ? ? ? ? ? ' ? ? ? ?</tr>' ? ? ? ? if (!val.trim()==''){ ? //去除輸入值左右的空格后不等于空,就將數(shù)據(jù)放進(jìn)表格中 ? ? ? ? ? ? $('#tab').append(name) ? ? ? ? } ? ? ? ? heroName.keyCode=function(){ ? ?//鍵盤點價事件 ? ? ? ? ? ? let e=window.event ? ? ? ? ? ? ? ? if (e.keyCode==13){ ? ? //回車后,自動提交 ? ? ? ? ? ? ? ? ? ? tab.submit() ? ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? $('tr:even').children().css('background-color','#f4fe56') ? ? ? ? $('tr:odd').children().css('background-color','#fe9d88') ? ? }) ? ? //上移--jq實現(xiàn) ? ? function up(btn) { ? ? ? ? //獲取當(dāng)前行的td ? ? ? ? var td1=$(btn).parent().prev() ? ? ? ? //var td1=btn.parentElement.previousElementSibling ? ? ? ? //獲取上一行的td ? ? ? ? var td2=$(btn).parent().parent().prev().children().eq(1) ? ? ? ? if(td2.html()=='人物'){ ? ? ? ? ? ? return ? ? ? ? } ? ? ? ? //交換兩個td的文本值 ? ? ? ? var t=td1.html(); ? ? ? ? td1.html(td2.html()) ? ? ? ? td2.html(t) ? ? } ? ? //下移--js實現(xiàn) ? ? function down(btn) { ? ? ? ? //獲取當(dāng)前行的td ? ? ? ? var td1=btn.parentElement.previousElementSibling ? ? ? ? //獲取下一行的td ? ? ? ? var td2=btn.parentElement.parentElement.nextElementSibling.firstElementChild.nextElementSibling ? ? ? ? //交換兩個td的文本值 ? ? ? ? var t=td1.innerHTML; ? ? ? ? td1.innerHTML=td2.innerHTML ? ? ? ? td2.innerHTML=t ? ? }
記得不要忘記添加jq的包喲
<script src="../jquery-3.3.1.min.js"></script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS控制表格隔行變色
- javascript實現(xiàn)table表格隔行變色的方法
- js實現(xiàn)鼠標(biāo)經(jīng)過表格行變色的方法
- 原生JS操作網(wǎng)頁給p元素添加onclick事件及表格隔行變色
- 一個簡單但常用的javascript表格樣式_鼠標(biāo)劃過行變色 簡潔實現(xiàn)
- javascript基于jQuery的表格懸停變色/恢復(fù),表格點擊變色/恢復(fù),點擊行選Checkbox
- 高效的表格行背景隔行變色及選定高亮的JS代碼
- JS實現(xiàn)的表格行上下移動操作示例
- 很弱的js表格換行效果(表格移動行)
- 鍵盤上下鍵移動選擇table表格行的js代碼
相關(guān)文章
JavaScript學(xué)習(xí)筆記之圖片庫案例分析
這篇文章主要介紹了JavaScript學(xué)習(xí)筆記之圖片庫案例,結(jié)合具體實例形式分析了javascript圖片庫相關(guān)的頁面元素動態(tài)操作實現(xiàn)技巧,需要的朋友可以參考下2019-01-01google廣告之另類js調(diào)用實現(xiàn)代碼
這篇文章主要介紹了google廣告之另類js調(diào)用實現(xiàn)代碼,需要的朋友可以參考下2020-08-08js 動態(tài)生成html 觸發(fā)事件傳參字符轉(zhuǎn)義的實例
下面小編就為大家?guī)硪黄猨s 動態(tài)生成html 觸發(fā)事件傳參字符轉(zhuǎn)義的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02JS實現(xiàn)彈出浮動窗口(支持鼠標(biāo)拖動和關(guān)閉)實例詳解
這篇文章主要介紹了JS實現(xiàn)彈出浮動窗口,可支持鼠標(biāo)拖動和關(guān)閉的功能,界面美觀大方,涉及javascript動態(tài)創(chuàng)建對話框的相關(guān)技巧,需要的朋友可以參考下2015-08-08BootstrapTable請求數(shù)據(jù)時設(shè)置超時(timeout)的方法
使用bootstrapTable獲取數(shù)據(jù)時,有時由于網(wǎng)絡(luò)或者服務(wù)器的原因,無法及時獲取到數(shù)據(jù),頁面顯示一直處于等待狀態(tài)。為了改善效果,考慮設(shè)置超時,請求發(fā)送后超時即顯示無數(shù)據(jù),過段時間重新發(fā)起請求2017-01-01