window對象屬性和方法
Window對象屬性
?首先,通過循環(huán)遍歷出window對象的所有屬性:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>php.cn</title> <script> //循環(huán)遍歷window對象的所有屬性 /* for(name|index in obj|arr){ } 描述:只能循環(huán)數(shù)組的下標(biāo),或?qū)ο蟮膶傩浴? 說明:如果循環(huán)數(shù)組的話,每次循將取下標(biāo)值。 對于數(shù)組中值為undefined的,不會循環(huán)。 循環(huán)數(shù)組,只返回有效的值。 如果循對象的話,每次循環(huán)取對象屬性。 嚴(yán)格的來說,對象中沒有方法一說,所有的都是屬性。 將一個函數(shù)賦給一個屬性后,這個屬性就變成方法了。 */ var i = 1; for(var name in window) { document.write(i+" "+name+"<br>"); i++; } </script> </head> <body> </body> </html>
name:指瀏覽器窗口的名字或框架的名字。這個名字是給a標(biāo)記的target屬性來用的。
設(shè)置窗口的名字:window.name = “newWin”
獲取窗口的名字:document.write(name);
top:代表最頂層窗口。如:window.top
parent:代表父級窗口,主要用于框架。
self:代表當(dāng)前窗口,主要用于框架中。
innerWidth:指瀏覽器窗口的內(nèi)寬(不含菜單欄、工具欄、地址欄、狀態(tài)欄),該屬性Firefox支持。
在IE下,使用 document.documentElement.clientWidth?來代替 window.innerWidth
innerHeight:指瀏覽器窗品的內(nèi)高(不含菜單欄、工具欄、地址欄、狀態(tài)欄),該屬性Firefox支持。
在IE下,使用 ?document.documentElement.clientHeight?來代替 ?window.innerHeight
document.documentElement ??就是<html>標(biāo)記對象
document.body ?就是 <body>標(biāo)記對象
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>php.cn</title> <script> //實例:測試當(dāng)前網(wǎng)頁的寬度和高度 //兼容所有瀏覽器 var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth; var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight; //輸出結(jié)果 document.write("寬度:"+width+",高度:"+height); </script> </head> <body> </body> </html>
window對象方法
alert():彈出一個警告對話框。
prompt():彈出一個輸入對話框。
confirm():彈出一個確認(rèn)對話框。如果單擊“確定按鈕”返回true,如果單擊“取消”返回false。
close():關(guān)閉窗口
print():打印窗口
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>php.cn</title> <script> function delect() { if(window.confirm("你確認(rèn)要刪除嗎?")){ //跳轉(zhuǎn)到指定刪除頁面執(zhí)行刪除操作 location.href="http://m.miracleart.cn"; } } </script> </head> <body> <a href="#" onClick="delect()">刪除</a> </body> </html>