标签:
Array对象的方法
//concat(arr)——数组合并
var Arr1 = new Array("a", "b", "c");
var Arr2 = new Array("d", "e");
var Arr3 = Arr1.concat(Arr2);
for(var i = 0; i < Arr3.length; i++)
document.write(Arr3[i] + "<br>");
//join(str)——使用str连接元素
var Arr = new Array("a", "b", "c");
var Result = Arr.join("--");
document.write(Result);
//pop()——删除最后一个元素
var Arr = new Array("a", "b", "c");
var Result = Arr.pop();
document.write(Result);
//push(data)——添加元素
var Arr = new Array("a", "b", "c");
Arr.push("d");
for(var i = 0; i < Arr.length; i++)
document.write(Arr[i] + "<br>");
//shift()——删除第一个元素
var Arr = new Array("a", "b", "c");
var Result = Arr.shift();
document.write(Result);
//unshift(data)——将data加入数组的前端
var Arr = new Array("a", "b", "c");
Arr.unshift("d");
for(var i = 0; i < Arr.length; i++)
document.write(Arr[i] + "<br>");
//reverse() 反转
var Arr = new Array("a", "b", "c");
Arr.reverse();
for(var i = 0; i < Arr.length; i++)
document.write(Arr[i] + "<br>");
//slice(start,end),返回下标start到下标end-1之间元素所形成的新数组
var Arr1 = new Array("a", "b", "c", "d", "e");
var Arr2 = Arr1.slice(1, 3);
for(var i = 0; i < Arr2.length; i++)
document.write(Arr2[i] + "<br>");
//sort()--排序,由小到大
var Arr = new Array(50, 40, 80, 90, 60);
Arr.sort();
for(var i = 0; i < Arr.length; i++)
document.write(Arr[i] + "<br>");
//toString()
var Arr = new Array("a", "b", "c");
var Result = Arr.toString();
document.write(Result);
Array作为参数
var Data1 = new Array(1, 2, 3, 4, 5);
var Data2 = new Array(10, 20, 30, 40, 50);
var Data3 = ArrAdd(Data1, Data2);
//在浏览器显示数组Data3的元素
for(var i = 0; i < Data3.length; i++)
document.write(Data3[i] + "<br>");
//声明一个名称为ArrAdd、有两个数组参数的函数
function ArrAdd(Arr1, Arr2)
{
var Arr3 = new Array();
for(var i = 0; i < Arr1.length; i++)
Arr3[i] = Arr1[i] + Arr2[i];
return Arr3;
}
2.9Error
var X = 100;
try //错误处理的开头
{
X = Y; //Y 未被定义将导致此程序语句发生错误
}
catch(e) //捕捉到Error对象e
{
document.write("捕捉到的Error对象错误码为" + e.number + "<br>");
document.write("捕捉到的Error对象错误信息为" + e.message + "<br>");
document.write("捕捉到的Error对象错误描述为" + e.description + "<br>");
}
finally //finally区块的程序代码一定会执行
{
document.write("X的值为" + X);
} //错误处理的结尾
3.环境对象
location对象包括目前开启之网页的网址信息(URI),可以通过该对象获取或控制浏览器的网址,重载网页或导向其他网页。<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>范例</title>
<script language="javascript">
for(var Property in window.location)
window.document.write(Property + ":" + window.location[Property] + "<br>");
</script>
</head>
<body>
<input type="button" value="重载"
onclick="javascript:window.location.reload();">
<input type="button" value="导向到百度网站"
onclick="javascript:window.location.replace(‘http://www.baidu.com‘);">
</body>
</html>
3.2screen对象
用户的屏幕信息。
window.document.write("height属性的值为" + screen.height + "<br>");
window.document.write("width属性的值为" + screen.width + "<br>");
window.document.write("availHeight属性的值为" + screen.availHeight + "<br>");
window.document.write("availWidth属性的值为" + screen.availWidth + "<br>");
window.document.write("colorDepth属性的值为" + screen.colorDepth + "<br>");
3.3navigator对象<!doctype html
<html>
<head>
<meta charset="utf-8">
<title>范例</title>
<script language="javascript">
for(var Property in window.navigator)
window.document.write(Property + ":" + window.navigator[Property] + "<br>");
</script>
</head>
</html>
3.4history对象
history对象包含浏览器的浏览历程记录。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>范例</title>
<script language="javascript">
for(var Property in window.history)
window.document.write(Property + ":" + window.history[Property] + "<br>");
</script>
</head>
<body>
<input type="button" value="上一页" onclick="javascript:window.history.back();">
<input type="button" value="下一页" onclick="javascript:window.history.forward();">
</body>
</html>
4.document对象
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>范例</title>
<script language="javascript">
function openDocument()
{
var NewWin = window.open("", "NewWin");//开启新的浏览器
NewWin.document.open("text/html");//在另一个浏览器开启新文件
NewWin.document.write("这是新的HTML文件"); //在新文件中显示此字符串
NewWin.document.close();//关闭新文件数据流
}
</script>
</head>
<body>
<input type="button" value="开启新文件" onclick="javascript:openDocument();">
</body>
</html>
<input type="checkbox" name="phone" id="CB1" value="hTC">
<input type="checkbox" name="phone" id="CB2" value="Apple">
<input type="checkbox" name="phone" id="CB3" value="ASUS">
var Element1=document.getElementById("CB1");
var Element2=document.getElementsByName("phone");
var Element3=document.getElementByTagName("input");
有属性: link;alink;vlink;background;bgColor,text;
document.body.bgColor="yellow";
有集合: all;anchors;links;forms;frames;Images;styleSheets;embeds;applets;plugins;
<form name="myForm1">
<input type="button" id="B1" value="按钮1">
<input type="button" id="B2" value="按钮2">
</form>
<form name="myForm2">
<input type="button" id="B3" value="按钮3">
<input type="button" id="B4" value="按钮4">
</form>
document.forms[0].B1.value;//获取值
document.forms.myForms1.B1.value;
document.forms[1].B3.value;
document.forms.myForms2.B3.value;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>范例</title>
<script language="javascript">
function showMsg()
{
var msg = document.getElementById("message");
msg.innerHTML = "Hello World!";
}
</script>
</head>
<body>
<input type="button" value="显示信息" onclick="javascript:showMsg();">
<p id="message"></p>
</body>
标签:
原文地址:http://www.cnblogs.com/songzhuzhu/p/5495351.html