1.路径符号的含义
src="/js/jquery.js"、"../"这个斜杠是绝对路径的意思,表示的是网站根目录.
其他的如"./ " 、 "../" 、 "jquery.js" 、 "js/jquery.js"等等表示的都是相对当前网页的路径,是相对路径。
2.获取网站的根目录
复制代码 代码如下:
function GetRootPath() {
var strFullPath = window.document.location.href;
var strPath = window.document.location.pathname;
var pos = strFullPath.indexOf(strPath);
var prePath = strFullPath.substring(0, pos);
var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
return (prePath + postPath);
}
3.获取url的参数
复制代码 代码如下:
//网站的 url如: http://www.A.COM?a=12
String.prototype.getQuery = function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = this.substr(this.indexOf("?") + 1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
var strHref = window.location.href;
alert(strHref.getQuery("a"));
4. js中的函数
4.1 Math.round 四捨五入
复制代码 代码如下:
document.write(Math.round(0.60) + "
") 1
document.write(Math.round(0.50) + "
") 1
document.write(Math.round(0.49) + "
") 0
document.write(Math.round(-4.40) + "
") -4
document.write(Math.round(-4.60)) -5
4.2 Math.random() 返回 0 到 1 之间的随机数。
复制代码 代码如下:
document.write(Math.random())
document.write(Math.floor(Math.random()*11)) Math 对象的 floor() 方法和 random() 来返回一个介于 0 和 10 之间的随机数
4.3 isNaN() 是否是非数字,如果是非数字true,否则false
4.4 Number() 把对象的值转换为数字
4.5 parseFloat() parseInt()如果字符串的第一个字符不能被转换为数字会返回 NaN
4.6 String() 函数把对象的值转换为字符串
5.数组
5.1 数组合併成数组concat合併数组,生成新的数组,原数组不变
复制代码 代码如下:
var arr = new Array(3)//定义数组
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
var arr1 = new Array(3)
arr1[0] = "James"
arr1[1] = "Adrew"
arr1[2] = "Martin"
var arr2=arr.concat(arr1))
5.2 数组合併成字符串join。默认是","连接的,可以指定,如join(".")
6. 正则表达式 最常用的是test(),找到是true,否则是false
复制代码 代码如下:
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
7.事件
7.1 onload 和 onUnload 页面加载,卸载时候调用
7.2 onFocus、onBlur 和 onChange 事件通常相互配合用来验证表单
7.3 onSubmit 用于在提交表单之前验证所有的表单域
复制代码 代码如下:



