DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> javascript 常用功能總結
javascript 常用功能總結
編輯:關於JavaScript     
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) + "<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 之間的隨機數。
復制代碼 代碼如下:
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 事件通常相互配合用來驗證表單
<input type="text" size="30" id="email" onchange="checkEmail()">
7.3 onSubmit 用於在提交表單之前驗證所有的表單域
復制代碼 代碼如下:
/*
下面是一個使用 onSubmit 事件的例子。當用戶單擊表單中的確認按鈕時,checkForm() 函數就會被調用。假若域的值無效,此次提交就會被取消。checkForm() 函數的返回值是 true 或者 false。如果返回值為true,則提交表單,反之取消提交。 */
<form method="post" action="xxx.htm" onsubmit="return checkForm()">

8. cookie
8.1 創建
復制代碼 代碼如下:
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. 計時
setTimeout() 開始計時
var t=setTimeout("javascript語句",毫秒) clearTimeout(t) //停止計時
10. 打開網站
10.1 在另一個窗口打開網站 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 在同一個窗口打開網站
window.location.href ='http://www.sohu.com' ;
11. 對象
11.1 對象定義,銷毀
復制代碼 代碼如下:
var oObject = new Object;
// do something with the object here
oObject = null;

11.2 定義類
復制代碼 代碼如下:
  function Cat(name,color){
    this.name = name;
    this.color = color;
    this.type = "貓科動物";
    this.eat = function(){alert("吃老鼠");};
  }

11.3 利用JSON去構造一個對象
復制代碼 代碼如下:
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去構造一個對象
復制代碼 代碼如下:
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);
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved