JS move li row data, click Up Move down (is the position of the interchange, not the top of the offset change)

Source: Internet
Author: User

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta content= "text/html; Charset=utf-8 "http-equiv=" Content-type ">
<title>js move Li row data, click on Move Down </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>

<body>
<div class= "Content" >

<ul id= "PCon1" >
<li id= "ID1" ><div class= "context" > Test data do you believe it, a</div></li>?
<li id= "Id2" ><div class= "context" > Test data do you believe it, b</div></li>?
<li id= "ID3" ><div class= "context" > Test data do you believe it, 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;
}
}
Dom does not provide a InsertAfter () method
function InsertAfter (newelement, targetelement) {

var parent = Targetelement.parentnode;

if (Parent.lastchild = = targetelement) {

If the last node is the target element, it is added directly. Because the default is the last
Parent.appendchild (newelement);

}

else {

Parent.insertbefore (newelement, targetelement.nextsibling);

If it is not, it is inserted in front of the next sibling node of the target element. Which is behind the target element.
}

}

function MyOrder (MYLIST,M,MO,MT) {
MyList for UL ID value, m for 0 display text, m for 1 display picture, MO, MT for text or picture content
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 ("PCon1", 0, "Move Up", "Move Down");
</script>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.