javascript数组方法的使用

来源:互联网 发布:西安西工大科信软件 编辑:程序博客网 时间:2024/05/23 15:39

push方法在数组尾部添加数据,unshift方法在数组头部添加数据。


join使用方法为 a.join("任意字符");

a.reverse(); 

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>数组方法之增加和删除</title><style type="text/css">*{margin:0}#show{width:200px;height:200px;margin:0 auto;color:white;background:#2c2c2c;text-align:center}</style><script type="text/javascript">var a = [22,4,111,33];/*a.push("<span style='color:red'>a</span>");a.pop();a.unshift("<span style='color:yellow'>b</span>");*///a.reverse();document.write("<div id='show'>");document.write(a.concat("www")+"<br/>");//document.write(a.join("*")+"<br/>");document.write(a);document.write("</div>");</script></head><body></body></html>


代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>数组方法</title><style type="text/css">*{margin:0}#show{width:200px;height:200px;margin:0 auto;color:white;background:#2c2c2c;text-align:center}</style><script type="text/javascript">//var a = [33,4,111,22];//a.slice(2);var a = [1,2,3,4,5,6,7,8,9];document.write("<div id='show'>");//document.write("结果为:"+a.slice(1,3)+"<br/>");document.write("结果为:"+a.splice(4,2,"a")+"<br/>");document.write(a.toString());document.write("</div>");</script></head><body></body></html>


0 0
原创粉丝点击