js常用內建對象、Dom對象、BOM對象

來源:互聯網
上載者:User

標籤:http   color   io   os   使用   ar   java   for   檔案   

11、html元素事件屬性中,如onclick="",雙引號裡可以是方法條用,可以是js代碼(無需加<script>標籤)

12、JavaScript內建 對象、屬性和方法(您也應該避免使用 JavaScript 內建的對象、屬性和方法的名稱作為 Javascript 的變數或函數名):
Array     Date     eval     function     hasOwnProperty
Infinity     isFinite     isNaN     isPrototypeOf     length
Math     NaN     name     Number     Object
prototype     String     toString     undefined     valueOf


13、Windows 保留關鍵字:(在 HTML 中,您必須(為了可移植性,您也應該這麼做)避免使用 HTML 和 Windows 對象和屬性的名稱作為 Javascript 的變數及函數名:)
alert     all     anchor     anchors     area
assign     blur     button     checkbox     clearInterval
clearTimeout     clientInformation     close     closed     confirm
constructor     crypto     decodeURI     decodeURIComponent     defaultStatus
document     element     elements     embed     embeds
encodeURI     encodeURIComponent     escape     event     fileUpload
focus     form     forms     frame     innerHeight
innerWidth     layer     layers     link     location
mimeTypes     navigate     navigator     frames     frameRate
hidden     history     image     images     offscreenBuffering
open     opener     option     outerHeight     outerWidth
packages     pageXOffset     pageYOffset     parent     parseFloat
parseInt     password     pkcs11     plugin     prompt
propertyIsEnum     radio     reset     screenX     screenY
scroll     secure     select     self     setInterval
setTimeout     status     submit     taint     text
textarea     top     unescape     untaint     window

14、HTML 事件控制代碼(除此之外,您還應該避免使用 HTML 事件控制代碼的名稱作為 Javascript 的變數及函數名。):
onblur     onclick     onerror     onfocus
onkeydown     onkeypress     onkeyup     onmouseover
onload     onmouseup     onmousedown     onsubmit

15、自訂對象:
通過 JavaScript,您能夠定義並建立自己的對象。
建立新對象有兩種不同的方法:
    定義並建立對象的執行個體
    使用函數來定義對象,然後建立新的對象執行個體
1)、建立對象,並賦值
person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";
相當於:var person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};

2)、本例使用函數來構造對象:注意調用建構函式時需要加new關鍵字調用
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

var myFather=new person("John","Doe",50,"blue");
var myMother=new person("Sally","Rally",48,"green");

3)把方法添加到 JavaScript 對象
方法只不過是附加在對象上的函數。
在構造器函數內部定義對象的方法:
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;

this.changeName=changeName;
function changeName(name)
{
this.lastname=name;
}
}

16.avaScript for...in 語句迴圈遍曆對象的屬性。(for...in 迴圈中的代碼塊將針對每個屬性執行一次。)
var person={fname:"Bill",lname:"Gates",age:56};

for (x in person)
{
txt=txt + person[x];
}
//txt=BillGates56

17、日期對象:
建立日期:
var d=new Date() // 當前日期和時間
var d=new Date(milliseconds) //返回從 1970 年 1 月 1 日至今的毫秒數
var d=new Date(dateString)
var d=new Date(year, month, day, hours, minutes, seconds, milliseconds)
設定日期
 var myDate=new Date();
myDate.setFullYear(2010,0,14);

在下面的例子中,我們將日期對象設定為 5 天后的日期:
var myDate=new Date();
myDate.setDate(myDate.getDate()+5);

日期對象的一些常用方法
var x1=d.getDay();    //回去當前日期為第幾天  從星期日到星期六 分別為 0~6
var x2=d.getFullYear(); //擷取年份  2014
var x3=d.getTime(); // 返回從 1970 年 1 月 1 日至今的毫秒數。
var h=today.getHours(); //擷取小時
var m=today.getMinutes(); //擷取分鐘    
var s=today.getSeconds();// 擷取秒數

在頁面顯示時間:
<script>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById(‘txt‘).innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}

function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</script>

在 onload中調用 startTime()

18、JavaScript Array(數組) 對象:
建立一個數組,有三種方法。
1: 常規方式:
var myCars=new Array();
myCars[0]="Saab";      
myCars[1]="Volvo";
myCars[2]="BMW";
2: 簡潔方式:
var myCars=new Array("Saab","Volvo","BMW");
3: 字面:
var myCars=["Saab","Volvo","BMW"];

訪問數組
var name=myCars[0];

在一個數組中你可以有不同的對象
所有的JavaScript變數都是對象。數組元素是對象。函數是對象。
因此,你可以在數組中有不同的變數類型。
你可以在一個數組中包含對象元素、函數、數組:
myArray[0]=Date.now;
myArray[1]=myFunction;
myArray[2]=myCars;

數組方法和屬性
使用數組對象預定義屬性和方法:
var x=myCars.length             // the number of elements in myCars
var y=myCars.indexOf("Volvo")   // the index position of "Volvo"

常用數組方法:
var fruits = ["Banana", "Orange", "Apple", "Mango"];

1、合并數組
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);

2、用數組的元素組成字串
x.innerHTML=fruits.join();

3、刪除數組的最後一個元素 - pop()
fruits.pop();

4、數組的末尾添加新的元素 - push()
fruits.push("Kiwi")

5、將一個數組中的元素的順序反轉排序 - reverse()
fruits.reverse();

6、刪除數組的第一個元素 - shift()
fruits.shift();

7、數組排序(按首字母順序升序)- sort()
fruits.sort();

8、數字排序(按數字順序升序)- sort()
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b});

9、數字排序(按數字順序降序)
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return b-a});

20、js中 Boolean 對象

Boolean 對象代表兩個值:"true" 或者 "false"

下面的代碼定義了一個名為 myBoolean 的布爾對象:
var myBoolean=new Boolean();

如果布爾對象無初始值或者其值為:

    0
    -0
    null
    ""
    false
    undefined
    NaN

那麼對象的值為 false。否則,其值為 true(即使當自變數為字串 "false" 時)

21、JavaScript Math(算數) 對象

1、四捨五入
Math.round(2.5); // 3

2、
如何使用 random() 來返回 0 到 1 之間的隨機數。
var x= Math.random(); //0.5183624960362349

3、取兩個數中的較大值
Math.max(5,10); //10

4、Math.min(5,10); //5

22、JavaScript RegExp 對象
文法:
var patt=new RegExp(pattern,modifiers);
or more simply:
var patt=/pattern/modifiers;

模式描述了一個運算式模型。
修飾符用於執行不區分大小寫和全文的搜尋。
i - 修飾符是用來執行不區分大小寫匹配。
g - 修飾符是用於執行全文的搜尋(而不是在找到第一個就停止尋找,而是找到所有的匹配)。

test()
The test()方法搜尋字串指定的值,根據結果並返回真或假。
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));   //返回true

exec() 方法檢索字串中的指定值。傳回值是被找到的值。如果沒有發現匹配,則返回 null。
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free")); //返回e


瀏覽器物件模型 (BOM)

23、Window 對象
所有瀏覽器都支援 window 對象。它表示瀏覽器視窗。
所有 JavaScript 全域對象、函數以及變數均自動成為 window 對象的成員。
全域變數是 window 對象的屬性。
全域函數是 window 對象的方法。
甚至 HTML DOM 的 document 也是 window 對象的屬性之一:

有三種方法能夠確定瀏覽器視窗的尺寸(瀏覽器的視口,不包括工具列和捲軸)。
var w=window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

其他 Window 方法

一些其他方法:
    window.open() - 開啟新視窗
    window.close() - 關閉當前視窗
    window.moveTo() - 移動當前視窗
    window.resizeTo() - 調整當前視窗的尺寸
    
24、window.screen 對象包含有關使用者螢幕的資訊。

    screen.availWidth - 可用的螢幕寬度
    screen.availHeight - 可用的螢幕高度
    
25、window.location 對象用於獲得當前頁面的地址 (URL),並把瀏覽器重新導向到新的頁面。

    location.hostname 返回 web 主機的網域名稱
    location.pathname 返回當前頁面的路徑和檔案名稱
    location.port 返回 web 主機的連接埠 (80 或 443)
    location.protocol 返回所使用的 web 協議(http:// 或 https://)
    location.href 屬性返回當前頁面的整個 URL。
    location.pathname 屬性返回 URL 的路徑名。
    location.assign(“url”) 方法載入新的文檔。
    
26、window.history 對象包含瀏覽器的曆史。

    history.back() - 與在瀏覽器點擊後退按鈕相同
    history.forward() - 與在瀏覽器中點擊按鈕向前相同
    
27、window.navigator 對象包含有關訪問者瀏覽器的資訊。

<script>
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";
document.getElementById("example").innerHTML=txt;
</script>

28、JavaScript 彈窗
可以在 JavaScript 中建立三種訊息框:警告框、確認框、提示框。

警告框
警告框經常用於確保使用者可以得到某些資訊。
當警告框出現後,使用者需要點擊確定按鈕才能繼續進行操作。
文法
window.alert("sometext");
window.alert() 方法可以不帶上window對象,直接使用alert()方法。


確認框
確認框通常用於驗證是否接受使用者操作。
當確認卡彈出時,使用者可以點擊 "確認" 或者 "取消" 來確定使用者操作。
當你點擊 "確認", 確認框返回 true, 如果點擊 "取消", 確認框返回 false。文法
window.confirm("sometext");
window.confirm() 方法可以不帶上window對象,直接使用confirm()方法。

提示框
提示框經常用於提示使用者在進入頁面前輸入某個值。
當提示框出現後,使用者需要輸入某個值,然後點擊確認或取消按鈕才能繼續操縱。
如果使用者點擊確認,那麼傳回值為輸入的值。如果使用者點擊取消,那麼傳回值為 null。
文法
window.prompt("sometext","defaultvalue");
window.prompt() 方法可以不帶上window對象,直接使用prompt()方法。

換行
彈窗使用 反斜線 + "n"(\n) 來設定換行。
執行個體
alert("Hellon\nHow are you?");

29、JavaScript 計時事件

通過使用 JavaScript,我們有能力作到在一個設定的時間間隔之後來執行代碼,而不是在函數被調用後立即執行。我們稱之為計時事件。

在 JavaScritp 中使用計時事件是很容易的,兩個關鍵方法是:

    setInterval() - 間隔指定的毫秒數不停地執行指定的代碼。
    setTimeout() - 暫停指定的毫秒數後執行指定的代碼
setInterval() 和 setTimeout() 是 HTML DOM Window對象的兩個方法。
第一個參數是函數(function)
第二個參數間隔的毫秒數

停止執行:
clearInterval() 方法用於停止 setInterval() 方法執行的函數代碼。
clearTimeout() 方法用於停止執行setTimeout()方法的函數代碼。

30、JavaScript Cookies
Cookies 用於儲存 web 頁面的使用者資訊。
JavaScript 可以使用 document.cookie 屬性來建立 、讀取、及刪除 cookies。

javaScript 中,建立 cookie 如下所示:
document.cookie="username=John Doe";
您還可以為 cookie 添加一個到期時間(以 UTC 或 GMT 時間)。預設情況下,cookie 在瀏覽器關閉時刪除:
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 GMT";
您可以使用 path 參數告訴瀏覽器 cookie 的路徑。預設情況下,cookie 屬於當前頁面。
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";

使用 JavaScript 讀取 Cookie
var x = document.cookie;
document.cookie 將以字串的方式返回所有的 cookies,類型格式: cookie1=value; cookie2=value; cookie3=value;

使用 JavaScript 修改 Cookie

在 JavaScript 中,修改 cookies 類似於建立 cookies,如下所示:
document.cookie="username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";  //舊的 cookie 將被覆蓋。

使用 JavaScript 刪除 Cookie

刪除 cookie 非常簡單。您只需要設定 expires 參數為以前的時間即可,如下所示,設定為 Thu, 01 Jan 1970 00:00:00 GMT:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT"; //注意,當您刪除時不必指定 cookie 的值。

Cookie 字串
document.cookie 屬性看起來像一個普通的文本字串,其實它不是。
即使您在 document.cookie 中寫入一個完整的 cookie 字串, 當您重新讀取該 cookie 資訊時,cookie 資訊是以名/值對的形式展示的。
如果您設定了新的 cookie,舊的 cookie 不會被覆蓋。 新 cookie 將添加到 document.cookie 中,所以如果您重新讀取document.cookie,您將獲得如下所示的資料:
cookie1=value; cookie2=value;

demo:
<!DOCTYPE html>
<html>
<head>
<script>

function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}

function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(‘;‘);
for(var i=0; i<ca.length; i++)
  {
  var c = ca[i].trim();
  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
return "";
}

function checkCookie()
{
var user=getCookie("username");
if (user!="")
  {
  alert("Welcome again " + user);
  }
else
  {
  user = prompt("Please enter your name:","");
  if (user!="" && user!=null)
    {
    setCookie("username",user,30);
    }
  }
}

</script>
</head>
<body onload="checkCookie()">
</body>
</html>

js常用內建對象、Dom對象、BOM對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.