<!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 http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
<title>javascript各种数组方法的使用</title>
<style>
p{color:green;padding:10px 15px;margin:12px 0;background:#f0f0f0;border:1px dotted #333;font:12px/1.5 Courier New;word-wrap:
break
-word;}
</style>
<script type=
"text/javascript"
>
window.onload =
function
()
{
var
ap = document.getElementsByTagName(
"p"
);
var
aInput = document.getElementsByTagName(
"input"
);
var
i = 0;
var
bS1 = bS2 = true;
var
aTmp = [];
aInput[0].onclick =
function
()
{
aTmp = getArray(ap[0].innerHTML);
bS1 ?
(aTmp.shift(), this.value = this.value.replace(
"删除"
,
"添加"
), bS1 = false) :
(aTmp.unshift(
"January(1)"
), this.value = this.value.replace(
"添加"
,
"删除"
), bS1 = true);
ap[0].innerHTML = aTmp.join()
};
aInput[1].onclick =
function
()
{
aTmp = getArray(ap[0].innerHTML);
bS2 ?
(aTmp.pop(), this.value = this.value.replace(
"删除"
,
"添加"
), bS2 = false) :
(aTmp.push(
"December(12)"
), this.value = this.value.replace(
"添加"
,
"删除"
), bS2 = true);
ap[0].innerHTML = aTmp.join()
};
aInput[2].onclick =
function
()
{
aTmp = getArray(ap[1].innerHTML);
ap[1].innerHTML = aTmp.concat(aTmp).toString().replace(/s/g,
""
)
};
aInput[3].onclick =
function
()
{
aTmp = getArray(ap[1].innerHTML);
aTmp.length = 10;
ap[1].innerHTML = aTmp.join()
};
aInput[4].onclick =
function
()
{
aTmp = [
"red"
,
"green"
,
"blue"
,
"white"
,
"yellow"
,
"black"
,
"brown"
];
ap[2].innerHTML = aTmp.join()
};
aInput[5].onclick =
function
()
{
aTmp = getArray(ap[2].innerHTML);
aTmp.splice(0, 3);
ap[2].innerHTML = aTmp.join()
};
aInput[6].onclick =
function
()
{
aTmp = getArray(ap[2].innerHTML);
aTmp.splice(1, 2);
ap[2].innerHTML = aTmp.join()
};
aInput[7].onclick =
function
()
{
aTmp = getArray(ap[2].innerHTML);
aTmp.splice(1, 0,
"orange"
,
"purple"
);
ap[2].innerHTML = aTmp.join()
};
aInput[8].onclick =
function
()
{
aTmp = getArray(ap[2].innerHTML);
aTmp.splice(1, 2,
"#009900"
,
"#0000ff"
);
ap[2].innerHTML = aTmp.join()
};
function
getArray(str)
{
aTmp.length = 0;
str = str.split(
","
);
for
(
var
i in str)aTmp.push(str[i]);
return
aTmp
}
}
</script>
</head>
<body>
<p>January(1),February(2),March(3),April(4),May(5),June(6),July(7),Aguest(8),September(9),October(10),November(11),December(12)</p>
<input type=
"button"
value=
"删除January(1)"
/>
<input type=
"button"
value=
"删除December(12)"
/>
<p>0,1,2,3,4,5,6,7,8,9</p>
<input type=
"button"
value=
"复制"
/>
<input type=
"button"
value=
"还原"
/>
<p>red,green,blue,white,yellow,black,brown</p>
<input type=
"button"
value=
"还原"
/>
<input type=
"button"
value=
"删除前三项"
/>
<input type=
"button"
value=
"删除第二至三项"
/>
<input type=
"button"
value=
"在第二项后插入(orange, purple)"
/>
<input type=
"button"
value=
"替换第二项和第三项"
/>
</body>
</html>