JS實(shí)現(xiàn)點(diǎn)擊上移下移LI行數(shù)據(jù)的方法
本文實(shí)例講述了JS實(shí)現(xiàn)點(diǎn)擊上移下移LI行數(shù)據(jù)的方法。分享給大家供大家參考。具體如下:
這里演示JavaScript排序功能,點(diǎn)擊按鈕實(shí)現(xiàn)數(shù)據(jù)的上移和下稱,一共有兩組測試效果,上組采用箭頭圖標(biāo)控制的方式,更美觀,下組是直接使用文字,根據(jù)你的需要自行選擇。myList為ul的id值,m為0顯示文字,m為1顯示圖片,mO、mT為文字或圖片內(nèi)容。
演示效果如下圖所示:

具體代碼如下:
<!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>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>JS移動(dòng)li行數(shù)據(jù),點(diǎn)擊上移下移</title>
<style type="text/css">
* {
padding:0;
margin:0;
}
.content {width:500px;margin:20px auto;}
#pCon {width:500px;list-style:none;}
#pCon li {height:20px;display:block;border-bottom:1px #ccc solid;}
#pCon li a{margin-left:5px;text-decoration:none;}
#pCon li a:hover{cursor:hand;}
.context {float:left;display:inline;}
.control {float:right;display:inline;}
.control img {width:50px;height:12px;overflow:hidden;float:left;display:inline;}
hr {margin:30px auto;}
#pCon1 {width:500px;list-style:none;}
#pCon1 li {height:20px;display:block;border-bottom:1px #ccc solid;}
#pCon1 li a{margin-left:5px;text-decoration:none;}
#pCon1 li a:hover{cursor:hand;}
</style>
</head>
<body>
<div class="content">
<ul id="pCon">
<li><div class="context">點(diǎn)擊右側(cè)箭頭上移下移A</div></li>
<li><div class="context">點(diǎn)擊右側(cè)箭頭上移下移B</div></li>
<li><div class="context">點(diǎn)擊右側(cè)箭頭上移下移C</div></li></ul>
<hr/>
<ul id="pCon1">
<li><div class="context">測試數(shù)據(jù)你相信么A</div></li>
<li><div class="context">測試數(shù)據(jù)你相信么B</div></li>
<li><div class="context">測試數(shù)據(jù)你相信么C</div></li>
</ul>
</div>
<script>
function moveSonU(tag,pc){
var tagPre=get_previoussibling(tag);
var t=document.getElementById(pc);
if(tagPre!=undefined){
t.insertBefore(tag,tagPre);
}
}
function moveSonD(tag){
var tagNext=get_nextsibling(tag);
if(tagNext!=undefined){
insertAfter(tag,tagNext);
}
}
function get_previoussibling(n){
if(n.previousSibling!=null){
var x=n.previousSibling;
while (x.nodeType!=1)
{
x=x.previousSibling;
}
return x;
}
}
function get_nextsibling(n){
if(n.nextSibling!=null){
var x=n.nextSibling;
while (x.nodeType!=1)
{
x=x.nextSibling;
}
return x;
}
}
function insertAfter(newElement,targetElement){
var parent=targetElement.parentNode;
if(parent.lastChild==targetElement){
parent.appendChild(newElement);
}else{
parent.insertBefore(newElement,targetElement.nextSibling);
}
}
function myOrder(myList,m,mO,mT){
//myList為ul的id值,m為0顯示文字,m為1顯示圖片,mO、mT為文字或圖片內(nèi)容
var pCon=document.getElementById(myList);
var pSon=pCon.getElementsByTagName("li");
for(i=0;i<pSon.length;i++){
var conTemp=document.createElement("div");
conTemp.setAttribute("class","control");
var clickUp=document.createElement("a");
var clickDown=document.createElement("a");
if(m==0){
var upCon=document.createTextNode(mO);
var downCon=document.createTextNode(mT);
}else{
var upCon=document.createElement("img");
var downCon=document.createElement("img");
upCon.setAttribute("src",mO);
downCon.setAttribute("src",mT);
}
clickUp.appendChild(upCon);
clickUp.setAttribute("href","#");
clickDown.appendChild(downCon);
clickDown.setAttribute("href","#");
pSon[i].appendChild(conTemp);
conTemp.appendChild(clickUp);
conTemp.appendChild(clickDown);
clickUp.onclick=function(){
moveSonU(this.parentNode.parentNode,myList);
}
clickDown.onclick=function(){
moveSonD(this.parentNode.parentNode);
}
}
}
myOrder("pCon",1,"http://img.jbzj.com/file_images/article/201508/201585153341254.png?201575153424","http://img.jbzj.com/file_images/article/201508/201585153353977.png?20157515349");
myOrder("pCon1",0,"上移","下移");
</script>
</body>
</html>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- 基于js實(shí)現(xiàn)數(shù)組相鄰元素上移下移
- js操作table中tr的順序?qū)崿F(xiàn)上移下移一行的效果
- JS與jQuery實(shí)現(xiàn)ListBox上移,下移,左移,右移操作功能示例
- js實(shí)現(xiàn)數(shù)組內(nèi)數(shù)據(jù)的上移和下移的實(shí)例
- Vue.js實(shí)現(xiàn)一個(gè)todo-list的上移下移刪除功能
- AngularJS實(shí)現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實(shí)例
- JS實(shí)現(xiàn)的表格操作類詳解(添加,刪除,排序,上移,下移)
- js實(shí)現(xiàn)Select列表各項(xiàng)上移和下移的方法
- JS實(shí)現(xiàn)一個(gè)列表中包含上移下移刪除等功能
- js實(shí)現(xiàn)按鈕進(jìn)行某行上移下移
相關(guān)文章
js或jquery動(dòng)態(tài)輸出select option的實(shí)現(xiàn)代碼
在 JavaScript 中動(dòng)態(tài)生成 <option> 元素并添加到 <select> 下拉列表中,可以通過以下幾種方式實(shí)現(xiàn),以下是不同場景下的完整代碼示例2025-03-03
JavaScript網(wǎng)絡(luò)請(qǐng)求工具庫axios使用實(shí)例探索
這篇文章主要為大家介紹了JavaScript網(wǎng)絡(luò)請(qǐng)求工具庫axios使用實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
小程序scroll-view安卓機(jī)隱藏橫向滾動(dòng)條的實(shí)現(xiàn)詳解
這篇文章主要介紹了小程序scroll-view安卓機(jī)隱藏橫向滾動(dòng)條的實(shí)現(xiàn)詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
javascript為下拉列表動(dòng)態(tài)添加數(shù)據(jù)項(xiàng)
這篇文章主要介紹了javascript如何為下拉列表動(dòng)態(tài)添加數(shù)據(jù)項(xiàng),需要的朋友可以參考下2014-05-05
使用OpenLayers3 添加地圖鼠標(biāo)右鍵菜單
這篇文章主要介紹了使用OpenLayers3 添加地圖鼠標(biāo)右鍵菜單的相關(guān)資料,需要的朋友可以參考下2015-12-12
微信小程序中webview組件的使用與應(yīng)用場景詳解
web-view組件是一個(gè)可以用來承載網(wǎng)頁的容器,會(huì)自動(dòng)鋪滿整個(gè)小程序頁面,這篇文章主要介紹了微信小程序中的webview組件的基本使用方法、應(yīng)用場景、通信機(jī)制以及注意事項(xiàng),需要的朋友可以參考下2025-03-03

