JavaScript的对象
1.数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var arr=new Array(1,2,3);
var arr2=[1,2,3];
arr2[0]=10;
var arr3=[1,2,3];
arr3[10]=10;
arr3[5]='hello';
var arr4=[1,2,3];
for(let i=0;i<arr4.length;i++) {
}
var arr5=[1,2,3];
arr5.push(10);
arr5.splice(0,1);
alert(arr5)
</script>
</body>
</html>
2.字符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var str=new String("hello")
var str2="Hello js"
alert(str.length);
var str2=" Hello js "
alert(1+str2.trim()+);
</script>
</body>
</html>
3.自定义对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var person={
name:"zhangsan",
age:20,
eat:function (){
alert("干饭~")
}
}
alert(person.name);
alert(person.age)
person.eat();
</script>
</body>
</html>
4、BOM对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
setTimeout(function (){
alert("未来可期!")
},2000);
setInterval(function (){
alert("未来可期!")
},2000);
</script>
</body>
</html>
5、History对象
6、location对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
document.write("三秒后跳转首页");
setTimeout(function (){
location.href="http://www.baidu.com";
},3000)
</script>
</body>
</html>
7、DOM对象
1.获取Element对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p id="demo"></p>
<div class="cls">唯有梦想不可被辜负</div>
<div class="cls">你最帅</div>
<input type="checkbox" name="hobby">电影
<input type="checkbox" name="hobby">旅游
<input type="checkbox" name="hobby">游戏
<script>
document.getElementById("demo").innerHTML="Hello js!";
var ns = document.getElementsByTagName("div");
for (let i = 0; i < ns.length; i++) {
ns[i].style.color='red';
}
alert(ns.length);
var byName = document.getElementsByClassName("cls");
for (let i = 0; i < byName.length; i++) {
alert(byName[i]);
}
var ns1 = document.getElementsByName("hobby");
alert(ns1.length);
</script>
</body>
</html>
|