整理的比较不错的JavaScript的方法和技巧第1/3页

适合阅读范围:对JavaScript一无所知~离精通只差一步之遥的人
基础知识:HTML


JavaScript就这么回事1:基础知识 

1 创建脚本块

1: 
2: JavaScript code goes here
3:  



2 隐藏脚本代码

1: 
2: 
5:  


在不支持JavaScript的浏览器中将不执行相关代码

3 浏览器不支持的时候显示

1:  



4 链接外部脚本文件

1:  


5 注释脚本

1: // This is a comment
2: document.write(“Hello”); // This is a comment
3: /*
4: All of this
5: is a comment
6: */ 



6 输出到浏览器

1: document.write(“Hello”); 



7 定义变量

1: var myVariable = “some value”; 



8 字符串相加

1: var myString = “String1” + “String2”; 



9 字符串搜索

1: 
2: 
7:  



10 字符串替换

1: thisVar.replace(“Monday”,”Friday”); 


11 格式化字串

1: 
2: 
21:  



12 创建数组

1: 
2: 
11:  



13 数组排序

1: 
2: 
11:  



14 分割字符串

1: 
2: 
10:  



15 弹出警告信息

1: 
2: 
5:  



16 弹出确认框

1: 
2: 
5:  



17 定义函数

1: 
2: 
8:  



18 调用JS函数

1: Link text
2: Link text 



19 在页面加载完成后执行函数

1: 
2: Body of the page
3:  


20 条件判断

1:  
21 指定次数循环

1:  



22 设定将来执行

1:  



23 定时执行函数

1:  



24 取消定时执行

1:  



25 在页面卸载时候执行函数

1: 
2: Body of the page
3:  

JavaScript就这么回事2:浏览器输出 


26 访问document对象

1: 
2: var myURL = document.URL;
3: window.alert(myURL);
4:  



27 动态输出HTML

1: 
2: document.write(“

Here's some information about this document:

”);
3: document.write(“
    ”);
    4: document.write(“
  • Referring Document: “ + document.referrer + “
  • ”);
    5: document.write(“
  • Domain: “ + document.domain + “
  • ”);
    6: document.write(“
  • URL: “ + document.URL + “
  • ”);
    7: document.write(“
”);
8:  


28 输出换行

1: document.writeln(“a”);
2: document.writeln(“b”); 



29 输出日期

1: 
2: var thisDate = new Date();
3: document.write(thisDate.toString());
4:  



30 指定日期的时区

1: 
2: var myOffset = -2;
3: var currentDate = new Date();
4: var userOffset = currentDate.getTimezoneOffset()/60;
5: var timeZoneDifference = userOffset - myOffset;
6: currentDate.setHours(currentDate.getHours() + timeZoneDifference);
7: document.write(“The time and date in Central Europe is: “ + currentDate.toLocaleString());
8:  

1 2 3 下一页 阅读全文

你可能感兴趣的:(整理的比较不错的JavaScript的方法和技巧第1/3页)