-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjs_in_object.html
More file actions
117 lines (110 loc) · 3.75 KB
/
js_in_object.html
File metadata and controls
117 lines (110 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body onload="startTime()">
<!--对象的概念:
Javascript中的所有事物都是对象,比如字符串,数组,数值,函数等,每个对象带有自身的属性和方法
自定义对象:
自定义对象有两种方法,一种是定义并创建对象实例,另一种是使用函数来定义对象,然后创建新的对象实例-->
<!--String对象
String对象用来处理已有的字符串,字符串可以使用双引号也可以使用单引号,常用方法有:
(1)在字符串中查找字符串:indexOf();查找子字符串是否存在,如果存在,返回第一次出现的位置,不存在返回-1
lastIndexOf():找到之后返回最后一个出现的位置
(2)内容匹配:match(),如果有这个字符串,则返回这个字符串,如果没有,返回空
(3)替换内容:replace(),有两个参数,用第二个参数替换第一个参数
(4)字符串大小写转换:toUpperCase()转换成大写,toLowerCase()转换成小写
(5)字符串转换成数组:split()
(6)charAt()
(7)charCodeAt()
(8)concat()
(9)fromCharCode()
(10)search()
(11)slice()
(12)substring()
(13)substr()
(14)valueOf()
常用属性有:length,prototype,constructor
-->
<!--
Date日期对象
常用方法:
getFullYear();获取年份
getTime():获取毫秒
setFullYear();设置具体的时间
getDay();获取星期
-->
<div id="time"></div>
<script>
// 直接定义对象,Object是所有对象的父类
people = new Object();
people.name = "zhangsan";
people.age = "13";
document.write("name:" + people.name + "\tage:" + people.age);
//另外一种对象赋值的方式,可以将上面的全部注销
people = {
name:"lisi",
age:"30"
}
document.write("name:" + people.name + "\tage:" + people.age);
//使用函数的方法创建对象
function people(name,age){
//通过this进行索引
this.name = name;
this.age = age;
}
son = new people("wangwu","50");
document.write("\nname:"+son.name + "\tage:" + son.age);
</script>
<script>
//String对象
var str = "Hello Word!!";
var str1 = "Hello my love,best";
document.write("字符串长度为:" + str.length + "<br/>");
document.write(str.lastIndexOf("!"));
document.write(str.match("Word1"));
document.write(str.replace("Hello","HI!"));
document.write(str.toUpperCase());
document.write(str.toLowerCase());
var s = str1.split(' ');
document.write(s[0] + " ," + s[1] + " ," + s[2]);
</script>
<script>
//Date对象
// var date = new Date();
// document.write(date + "<br/>");
// document.write(date.getFullYear() + "<br/>");
// //获取毫秒数,从1970.1.1日开始
// document.write(date.getTime() + "<br/>");
// //设定时间,三个参数分别是:年月日
// date.setFullYear(2110,10,10)
// document.write(date + "<br/>");
//做一个时钟案例
function startTime(){
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
h = addzero(h);
m = addzero(m);
s = addzero(s);
document.getElementById("time").innerHTML = h + ":" + m + ":" + s;
//调用setTimeout()方法用于在指定的毫秒数后调用函数或计算表达式。
// 语法: setTimeout(code,millisec)
// 参数:
// code (必需):要调用的函数后要执行的 JavaScript 代码串。
// millisec(必需):在执行代码前需等待的毫秒数。
t = setTimeout(function(){
startTime();
},500);
}
function addzero(i){
if(i < 10)
i = "0" + i;
return i;
}
</script>
</body>
</html>