javascript 常用功能總結(jié)
更新時(shí)間:2012年03月18日 13:57:32 作者:
javascript 常用功能總結(jié),學(xué)習(xí)js的朋友可以參考下
1.路徑符號(hào)的含義
src="/js/jquery.js"、"../"這個(gè)斜杠是絕對(duì)路徑的意思,表示的是網(wǎng)站根目錄.
其他的如"./ " 、 "../" 、 "jquery.js" 、 "js/jquery.js"等等表示的都是相對(duì)當(dāng)前網(wǎng)頁(yè)的路徑,是相對(duì)路徑。
2.獲取網(wǎng)站的根目錄
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的參數(shù)
//網(wǎng)站的 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中的函數(shù)
4.1 Math.round 四捨五入
document.write(Math.round(0.60) + "<br />") 1
document.write(Math.round(0.50) + "<br />") 1
document.write(Math.round(0.49) + "<br />") 0
document.write(Math.round(-4.40) + "<br />") -4
document.write(Math.round(-4.60)) -5
4.2 Math.random() 返回 0 到 1 之間的隨機(jī)數(shù)。
document.write(Math.random())
document.write(Math.floor(Math.random()*11)) Math 對(duì)象的 floor() 方法和 random() 來(lái)返回一個(gè)介于 0 和 10 之間的隨機(jī)數(shù)
4.3 isNaN() 是否是非數(shù)字,如果是非數(shù)字true,否則false
4.4 Number() 把對(duì)象的值轉(zhuǎn)換為數(shù)字
4.5 parseFloat() parseInt()如果字符串的第一個(gè)字符不能被轉(zhuǎn)換為數(shù)字會(huì)返回 NaN
4.6 String() 函數(shù)把對(duì)象的值轉(zhuǎn)換為字符串
5.數(shù)組
5.1 數(shù)組合併成數(shù)組concat合併數(shù)組,生成新的數(shù)組,原數(shù)組不變
var arr = new Array(3)//定義數(shù)組
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 數(shù)組合併成字符串join。默認(rèn)是","連接的,可以指定,如join(".")
6. 正則表達(dá)式 最常用的是test(),找到是true,否則是false
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
7.事件
7.1 onload 和 onUnload 頁(yè)面加載,卸載時(shí)候調(diào)用
7.2 onFocus、onBlur 和 onChange 事件通常相互配合用來(lái)驗(yàn)證表單
<input type="text" size="30" id="email" onchange="checkEmail()">
7.3 onSubmit 用于在提交表單之前驗(yàn)證所有的表單域
/*
下面是一個(gè)使用 onSubmit 事件的例子。當(dāng)用戶(hù)單擊表單中的確認(rèn)按鈕時(shí),checkForm() 函數(shù)就會(huì)被調(diào)用。假若域的值無(wú)效,此次提交就會(huì)被取消。checkForm() 函數(shù)的返回值是 true 或者 false。如果返回值為true,則提交表單,反之取消提交。 */
<form method="post" action="xxx.htm" onsubmit="return checkForm()">
8. cookie
8.1 創(chuàng)建
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
8.2 讀取
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
9. 計(jì)時(shí)
setTimeout() 開(kāi)始計(jì)時(shí)
var t=setTimeout("javascript語(yǔ)句",毫秒) clearTimeout(t) //停止計(jì)時(shí)
10. 打開(kāi)網(wǎng)站
10.1 在另一個(gè)窗口打開(kāi)網(wǎng)站 window.open()
function openW(v){
var str = 'width=200,height=200,left=200,top=200,status=no,scrollbars=no,'
str += 'menubar=no,toolbar=no,resizable=no,location=no'
window.open(v,'',str);
}
10.2 在同一個(gè)窗口打開(kāi)網(wǎng)站
window.location.href ='http://www.sohu.com' ;
11. 對(duì)象
11.1 對(duì)象定義,銷(xiāo)毀
var oObject = new Object;
// do something with the object here
oObject = null;
11.2 定義類(lèi)
function Cat(name,color){
this.name = name;
this.color = color;
this.type = "貓科動(dòng)物";
this.eat = function(){alert("吃老鼠");};
}
11.3 利用JSON去構(gòu)造一個(gè)對(duì)象
var People = {
Create: function (name, age) {
this.name = name;
this.age = age;
},
SayHello: function () {
alert("Hello,My name is " + this.name + ".I am " + this.age);
}
};
11.4 利用prototype去構(gòu)造一個(gè)對(duì)象
var Person = function (name, age) {
this.name = name;
this.age = age;
};
Person.prototype.Introduce = function () {
alert("My name is " + this.name + ".I'm " + this.age);
}
src="/js/jquery.js"、"../"這個(gè)斜杠是絕對(duì)路徑的意思,表示的是網(wǎng)站根目錄.
其他的如"./ " 、 "../" 、 "jquery.js" 、 "js/jquery.js"等等表示的都是相對(duì)當(dāng)前網(wǎng)頁(yè)的路徑,是相對(duì)路徑。
2.獲取網(wǎng)站的根目錄
復(fù)制代碼 代碼如下:
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的參數(shù)
復(fù)制代碼 代碼如下:
//網(wǎng)站的 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中的函數(shù)
4.1 Math.round 四捨五入
復(fù)制代碼 代碼如下:
document.write(Math.round(0.60) + "<br />") 1
document.write(Math.round(0.50) + "<br />") 1
document.write(Math.round(0.49) + "<br />") 0
document.write(Math.round(-4.40) + "<br />") -4
document.write(Math.round(-4.60)) -5
4.2 Math.random() 返回 0 到 1 之間的隨機(jī)數(shù)。
復(fù)制代碼 代碼如下:
document.write(Math.random())
document.write(Math.floor(Math.random()*11)) Math 對(duì)象的 floor() 方法和 random() 來(lái)返回一個(gè)介于 0 和 10 之間的隨機(jī)數(shù)
4.3 isNaN() 是否是非數(shù)字,如果是非數(shù)字true,否則false
4.4 Number() 把對(duì)象的值轉(zhuǎn)換為數(shù)字
4.5 parseFloat() parseInt()如果字符串的第一個(gè)字符不能被轉(zhuǎn)換為數(shù)字會(huì)返回 NaN
4.6 String() 函數(shù)把對(duì)象的值轉(zhuǎn)換為字符串
5.數(shù)組
5.1 數(shù)組合併成數(shù)組concat合併數(shù)組,生成新的數(shù)組,原數(shù)組不變
復(fù)制代碼 代碼如下:
var arr = new Array(3)//定義數(shù)組
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 數(shù)組合併成字符串join。默認(rèn)是","連接的,可以指定,如join(".")
6. 正則表達(dá)式 最常用的是test(),找到是true,否則是false
復(fù)制代碼 代碼如下:
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
7.事件
7.1 onload 和 onUnload 頁(yè)面加載,卸載時(shí)候調(diào)用
7.2 onFocus、onBlur 和 onChange 事件通常相互配合用來(lái)驗(yàn)證表單
<input type="text" size="30" id="email" onchange="checkEmail()">
7.3 onSubmit 用于在提交表單之前驗(yàn)證所有的表單域
復(fù)制代碼 代碼如下:
/*
下面是一個(gè)使用 onSubmit 事件的例子。當(dāng)用戶(hù)單擊表單中的確認(rèn)按鈕時(shí),checkForm() 函數(shù)就會(huì)被調(diào)用。假若域的值無(wú)效,此次提交就會(huì)被取消。checkForm() 函數(shù)的返回值是 true 或者 false。如果返回值為true,則提交表單,反之取消提交。 */
<form method="post" action="xxx.htm" onsubmit="return checkForm()">
8. cookie
8.1 創(chuàng)建
復(fù)制代碼 代碼如下:
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
8.2 讀取
復(fù)制代碼 代碼如下:
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
9. 計(jì)時(shí)
setTimeout() 開(kāi)始計(jì)時(shí)
var t=setTimeout("javascript語(yǔ)句",毫秒) clearTimeout(t) //停止計(jì)時(shí)
10. 打開(kāi)網(wǎng)站
10.1 在另一個(gè)窗口打開(kāi)網(wǎng)站 window.open()
復(fù)制代碼 代碼如下:
function openW(v){
var str = 'width=200,height=200,left=200,top=200,status=no,scrollbars=no,'
str += 'menubar=no,toolbar=no,resizable=no,location=no'
window.open(v,'',str);
}
10.2 在同一個(gè)窗口打開(kāi)網(wǎng)站
window.location.href ='http://www.sohu.com' ;
11. 對(duì)象
11.1 對(duì)象定義,銷(xiāo)毀
復(fù)制代碼 代碼如下:
var oObject = new Object;
// do something with the object here
oObject = null;
11.2 定義類(lèi)
復(fù)制代碼 代碼如下:
function Cat(name,color){
this.name = name;
this.color = color;
this.type = "貓科動(dòng)物";
this.eat = function(){alert("吃老鼠");};
}
11.3 利用JSON去構(gòu)造一個(gè)對(duì)象
復(fù)制代碼 代碼如下:
var People = {
Create: function (name, age) {
this.name = name;
this.age = age;
},
SayHello: function () {
alert("Hello,My name is " + this.name + ".I am " + this.age);
}
};
11.4 利用prototype去構(gòu)造一個(gè)對(duì)象
復(fù)制代碼 代碼如下:
var Person = function (name, age) {
this.name = name;
this.age = age;
};
Person.prototype.Introduce = function () {
alert("My name is " + this.name + ".I'm " + this.age);
}
您可能感興趣的文章:
相關(guān)文章
uniapp小程序底部tabbar圖標(biāo)大小設(shè)置辦法
這篇文章主要給大家介紹了關(guān)于uniapp小程序底部tabbar圖標(biāo)大小設(shè)置辦法的相關(guān)資料,在使用uniapp進(jìn)行開(kāi)發(fā)時(shí),tabbar是我們使用的很頻繁的一個(gè)組件,但是在特定的平臺(tái)會(huì)有一些使用上的限制,需要的朋友可以參考下2023-08-08
JavaScript函數(shù)、方法、對(duì)象代碼
函數(shù)定義可以嵌套在其他函數(shù)中,常用作子函數(shù)。但不能出現(xiàn)在循環(huán)或條件語(yǔ)句中。2008-10-10
JavaScript中Hoisting詳解 (變量提升與函數(shù)聲明提升)
函數(shù)聲明和變量聲明總是被JavaScript解釋器隱式地提升(hoist)到包含他們的作用域的最頂端。下面這篇文章主要給大家介紹了關(guān)于JavaScript中Hoisting(變量提升與函數(shù)聲明提升)的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08
JS根據(jù)Unix時(shí)間戳顯示發(fā)布時(shí)間是多久前【項(xiàng)目實(shí)測(cè)】
小編最近在實(shí)現(xiàn)這樣的需求類(lèi)似微信朋友圈顯示發(fā)布時(shí)間為距離當(dāng)前時(shí)間多久之前這樣的功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2019-07-07
vue3+Pinia+TypeScript?實(shí)現(xiàn)封裝輪播圖組件
這篇文章主要介紹了vue3+Pinia+TypeScript?實(shí)現(xiàn)封裝輪播圖組件,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
Javascript中將變量轉(zhuǎn)換為字符串的三種方法
這篇文章主要給大家介紹了關(guān)于Javascript中將變量轉(zhuǎn)換為字符串的三種方法,這三種方法分別是:value.toString()、"" + value和String(value),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
javascript替換已有元素replaceChild()使用介紹
這篇文章主要介紹了javascript替換已有元素replaceChild()使用,需要的朋友可以參考下2014-04-04
Javascript獲取HTML靜態(tài)頁(yè)面參數(shù)傳遞值示例
獲取HTML靜態(tài)頁(yè)面參數(shù)傳遞值可以利用split函數(shù)來(lái)按參數(shù)切成數(shù)組、利用正則表達(dá)式來(lái)獲取,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下2013-08-08

