Lyrics


< More and better />


note-taking

mydate
1
2
3
document.write("当前时间:"+mydate+"<br>");
mydate.setTime(mydate.getTime() + 60 * 60 * 1000);
document.write("推迟一小时时间:" + mydate);

当前时间:Tue Aug 02 2016 16:20:02 GMT+0800 (中国标准时间)

推迟一小时时间:Tue Aug 02 2016 17:20:02 GMT+0800 (中国标准时间)

#####获得星期

mydate
1
2
var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; 0~6
document.write("今天是:" + weekday[mydate.getDay()]);

今天是星期二
getDay();获取星期数 0~6;

#####获得年份,月份,日,点,分,秒

mydate
1
2
3
4
5
6
document.write("年份:"+mydate.getFullYear()+'<br>' );
document.write("月份:"+mydate.getMonth()+'<br>');
document.write("日期:"+mydate.getDay()+'<br>');
document.write("时:"+mydate.getHours()+'<br>');
document.write("分:"+mydate.getMinutes()+'<br>');
document.write("秒:"+mydate.getSeconds()+'<br>');
  1. 使用 String 对象的 toUpperCase() 方法来将字符串小写 字母转换为大写

  2. 使用toLowerCase()方法,将字符串所有大写字母都变成小写的字符串。

string对象

‘’’ var mystr=”I love JavaScript!”
document.write(mystr.charAt(3) ); ‘’’

  1. charAt();获取字符,参数字符的位置,从0开始,空格也算是一个字符

  2. indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置;,参数2个,寻找的字符,起始位置

    mystr
    1
    document.write(mystr.indexOf('o',5)); // 使用indexOf()方法,检索第二个字符o出现的位置。
  3. split() 方法将字符串分割为字符串数组,并返回此数组。

参数2个,分隔的字符例如(,),和分隔后保存的字符个数;可以这样理解,分割时按照指定字符分割,后面那个参数表示保留多少个字符串

1
2
3
4
5
6
var mystr = "www.imooc.com";
document.write(mystr.split(".")+"<br>");
document.write(mystr.split(".", 2)+"<br>");
运行结果:
www,imooc,com
www,imooc

  1. substring() 方法用于提取字符串中介于两个指定下标之间的字符。

  2. Object.substring(字符串起始位置,字符串结束位置) 理解:结果【 )

  3. substr() 方法从字符串中提取从 startPos位置开始的指定数目的字符串。
    Object.substr(startPos,length);

Math()方法:

所有的数学库函数,使用形式都为Math.abs()等

向上取整ceil();Math.ceil(); 都以0为基准, 因为document.write(Math.ceil(-3.9)); -3

向下取整floor() :Math.floor(),

四舍五入:round():Math.round(); 如果 x 与两侧整数同等接近,则结果接近 +∞方向的数字值 。(如 -5.5 将舍入为 -5; -5.52 将舍入为 -6),

random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。
获得0 ~ 10之间的随机数,代码如下:

document.write((Math.random())*10);

比较大小
升序:

sortNum(a,b) {
1
2
3
4
5
return a-b;
}
var myarr = new Array("80","16","50","6","100","1");
document.write(myarr.sort(sortNum));
![alt text](/img/535483720001a54506670563.jpg "Title")
计时器setInterval()

在执行时,从载入页面后每隔指定的时间执行代码。

语法:
setInterval(代码,交互时间);

调用函数格式(假设有一个clock()函数):

setInterval(“clock()”,1000) 或 var i=setInterval(clock,1000)

取消计时器clearInterval()

clearInterval() 方法可取消由 setInterval() 设置的交互时间。

语法:
clearInterval(i);

时器setTimeout()

setTimeout()计时器,在载入后延迟指定时间后,去执行一次表达式,仅执行一次。
语法:
setTimeout(代码,延迟时间);自动触发

‘’’ <title>计时器</title>
</head>
<script type=”text/javascript”>
var num=0;
var i;
function startCount(){
document.getElementById(‘count’).value=num;
num=num+1;
i=setTimeout(“startCount()”,1000);
}
function stopCount(){
clearTimeout(i);
}
</script >
</head>
<body>
<form>
<input type=”text” id=”count” />
<input type=”button” value=”Start” onclick=”startCount()” />
<input type=”button” value=”Stop” onclick=”stopCount()” />
</form>
</body>
</html> ```

History 对象

history对象记录了用户曾经浏览过的页面(URL),并可以实现浏览器前进与后退相似导航的功能。
注意:从窗口被打开的那一刻开始记录,每个浏览器窗口、每个标签页乃至每个框架,都有自己的history对象与特定的window对象关联。
语法:
window.history.[属性|方法]