js怎么让两个数值相互交换

JavaScript013

js怎么让两个数值相互交换,第1张

var temp// 声明了一个临时变量为空

var apple1 = '青苹果'

var apple2 = '红苹果'

temp = apple1// 把右边给左边

apple1 = apple2

apple2 = temp

console.log(apple1)

console.log(apple2)

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=gb2312">

<title>Select列表左右交换内容的JS</title>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

sortitems = 1  

function move(fbox,tbox) {

for(var i=0 i<fbox.options.length i++) {

if(fbox.options[i].selected  &&  fbox.options[i].value != "") {

var no = new Option()

no.value = fbox.options[i].value

no.text = fbox.options[i].text

tbox.options[tbox.options.length] = no

fbox.options[i].value = ""

fbox.options[i].text = ""

   }

}

BumpUp(fbox)

if (sortitems) SortD(tbox)

}

function BumpUp(box)  {

for(var i=0 i<box.options.length i++) {

if(box.options[i].value == "")  {

for(var j=i j<box.options.length-1 j++)  {

box.options[j].value = box.options[j+1].value

box.options[j].text = box.options[j+1].text

}

var ln = i

break

   }

}

if(ln < box.options.length)  {

box.options.length -= 1

BumpUp(box)

   }

}

function SortD(box)  {

var temp_opts = new Array()

var temp = new Object()

for(var i=0 i<box.options.length i++)  {

temp_opts[i] = box.options[i]

}

for(var x=0 x<temp_opts.length-1 x++)  {

for(var y=(x+1) y<temp_opts.length y++)  {

if(temp_opts[x].text > temp_opts[y].text)  {

temp = temp_opts[x].text

temp_opts[x].text = temp_opts[y].text

temp_opts[y].text = temp

temp = temp_opts[x].value

temp_opts[x].value = temp_opts[y].value

temp_opts[y].value = temp

      }

   }

}

for(var i=0 i<box.options.length i++)  {

box.options[i].value = temp_opts[i].value

box.options[i].text = temp_opts[i].text

   }

}

// End -->

</script>

</head>

<body>

<form ACTION="" METHOD="POST">

<table border="0">

<tr>

<td><select multiple size="5" name="list1">

<option value="ASP">ASP </option>

<option value="PHP"> PHP</option>

<option value="JSP"> JSP</option>

</select></td>

<td>

<input type="button" value=" >> " onclick="move(this.form.list1,this.form.list2)" name="B1">

<input type="button" value=" << " onclick="move(this.form.list2,this.form.list1)" name="B2">

</td>

<td><select multiple size="5" name="list2">

<option value="JAVA">JAVA</option>

<option value="DELPHI">DELPHI</option>

<option value="C++">C++</option>

</select></td>

</tr>

</table>

</form>

<p>可以同时多个左右移动</p>

</body>

</html>

<div id='div1'>xxx</div>

<div id='div2'>yyy</div>

<script>

var d1=document.getElementById('div1').innerHTML

var d2=document.getElementById('div2').innerHTML

document.getElementById('div2').innerHTML=d1

document.getElementById('div1').innerHTML=d2

</script>