1.BOM的概述
browser object modal :瀏覽器對象模型。
瀏覽器對象:window對象。
Window 對象會在 <body> 或 <frameset> 每次出現時被自動創建。
BOM Navigator 獲取當前所用的瀏覽器信息
BOM Screen 獲取當前所用的屏幕的信息
2. window的屬性-祖宗對象
innerHeight:
innerWidth: IE不支持通用寫法:document.body.clientWidth(window被省略)
document.body.clientHeight
self :等同於window對象
opener:是打開窗口的父窗口對象。(貌似谷歌不支持,害我調了那麼久)
2種情況下使用opener:
1.使用winodw.open()方法打開的頁面
2.超鏈(裏面的target屬性要設置成_blank)
示例:數據子傳父
父親頁面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>opener屬性</title>
</head>
<script type="text/javascript">
<!--
function fun(){
self.open("sub1.html");
}
//-->
</script>
<body>
傳來的數據:<input type="text" id="txt" /><br />
<!--<a href="sub1.html" target="_blank">打開子窗口</a>-->
<input type="button" value="打開子頁面" οnclick="fun()" />
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sub1</title>
</head>
<script type="text/javascript">
<!--
function fun(){
var text = document.getElementById("txt").value;
var hl= window.opener;
var txt = hl.document.getElementById("txt");
txt.value = text;
}
//-->
</script>
<body>
要傳遞的數據:<input type="text" id="txt" />
<input type="button" value="把數據傳遞到父窗口" οnclick="fun()" />
</body>
</html>
parent:是打開窗口的父窗口對象。
2種情況下使用parent:
1.iframe 框架
2.frame 框架
frames[]: 數組類型,代表子窗口的window對象的集合,可以通過frames[索引]拿到子窗口的window對象。
示例:父子窗口相互傳參.
open方法,是打開一個頁面.
示例:數據父子互傳
父親頁面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>parent屬性</title>
</head>
<script type="text/javascript">
<!--
function fun(){
var txt = document.getElementById("txt").value;
var son = window.frames[0];
var t = son.document.getElementById("txt");
t.value = txt;
}
//-->
</script>
<body>
數據:<input type="text" id="txt" /><br />
<input type="button" value="傳給子窗口" οnclick="fun()" /><br />
<iframe src="sub2.html" ></iframe>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sub2</title>
</head>
<script type="text/javascript">
<!--
function fun(){
var txt = document.getElementById("txt").value;
var p = window.parent;
var t = p.document.getElementById("txt"); //此處的t爲引用類型的object,因此可以改變父親文本框的內容
t.value = txt;
/* 錯誤示例!!!
var t = p.document.getElementById("txt").value; //此處的t僅爲string類型變量不能改變父親文本框內容!!!
t = txt;
*/
}
//-->
</script>
<body>
數據:<input type="text" id="txt" /><br />
<input type="button" value="傳給父窗口" οnclick="fun()" /><br />
</body>
</html>
對話框:
1)消息框 alert() ; ----排錯用
2)確認框 confirm() ; ----返回boolean型
while(true){
if(confirm("你愛我嗎?") == false)
continue;
break;
}
3)輸入框 prompt() ; (瞭解) ----返回輸入的字符串
var a = prompt("請輸入年齡",12);
alert(a);
定時器:
setTimeout ,setInterval
定時器: 1.setTimeout() :只會調用1次 因此一般寫在函數內部,每次調用都產生一個計時器,當跑完了就自動銷燬了
clearTimeout():銷燬setTimeout()計時器
2.setInterval() :每隔一段時間調用1次
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>window對象的計時器</title>
</head>
<script type="text/javascript">
<!--
/*
window對象的計時器:1.setTimeout(): 隔一段時間調用某個函數(只調用一次),返回的是一個計時器(理解成一個手錶)
clearTimeout() :銷燬由setTimeout()產生的計時器
2.setInterval(): 每隔一段時間調用某個函數(多次調用)
clearInterval(): 銷燬由setInterval()產生的計時器
*/
var t ;
var t1 ;
function fun(){
//拿到p標籤的主體內容
var p= document.getElementById("p") ;
var v = p.innerHTML ;
//將主體內類轉換爲number,轉換後賦值回去
p.innerHTML = v*1-1 ;
t = setTimeout("fun()",1000) ;
}
function fun1(){
//拿到p標籤的主體內容
var p= document.getElementById("p") ;
var v = p.innerHTML ;
//將主體內類轉換爲number,轉換後賦值回去
p.innerHTML = v*1-1 ;
}
function fun2(){
clearTimeout(t) ;
}
t1 = setInterval("fun1()",1000);
function fun3(){
clearInterval(t1) ;
}
function fun4(){
t1 = setInterval("fun1()",1000);
}
//-->
</script>
<body>
<p id = "p">10</p><br>
<input type="button" value="演示setTimeout方法" οnclick="fun()">
<input type="button" value="演示clearTimeout方法" οnclick="fun2()">
<input type="button" value="演示setInterval方法" οnclick="fun4()">
<input type="button" value="演示clearInterval方法" οnclick="fun3()">
</body>
</html>
模態窗體:
對話框一般分爲兩種類型:模態類型(modal)與非模態類型(modeless)。
所謂模態對話框,就是指除非採取有效的關閉手段,用戶的鼠標焦點或者輸入光標將一直停留在其上的對話框。
非模態對話框則不會強制此種特性,用戶可以在當前對話框以及其他窗口間進行切換。
window.showModalDialog("你好");
self.showModelessDialog("你好");
區別只是版本不同,第一個舊版本。
3. history對象
存儲了訪問過的對象
a. forward()前進
b. back() 後退
c. go(n) 正數是前進,負數是後退.
4. location對象。
1.href 屬性: 是指要連接到其他的URL
寫法一、window.location.href='demo_window對象的close方法.html' ;
寫法二、window.location='demo_window對象的close方法.html' ;
2.reload方法: 刷新
寫法: window.location.reload() ;
5.常用事件
a、鼠標移動事件****onmousemove(event) : 鼠標移動事件 event是事件對象。名字固定
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠標的移動事件</title>
<style type="text/css">
div{
width:300px;
height:300px;
border:1px solid red;
}
</style>
</head>
<script type="text/javascript">
<!--
function fun(e){
var x = e.clientX;
var y = e.clientY;
var p=document.getElementById("txt");
p.value = x +":" + y;
}
//-->
</script>
<body>
<input type="text" id= "txt" />
<div οnmοusemοve="fun(event)">ss</div>
</body>
</html>
動態註冊:****onmouseover : 鼠標懸停事件
this: 把this寫在那個標籤裏面就代表那個標籤對象 this是包含它的函數作爲方法被調用時所屬的對象。
this.style.backgroundColor : 當調用樣式表中的屬性的時候,如果屬性中間有橫槓,則應去掉.
btn.οnclick=function (){
img.style.display="none";
this.disabled="disabled";
};
function fun(obj){
obj.value="小白";
}
<input type="button" value="搜狐" id="btn2" οnclick="fun(this)" />
****onmouseout : 鼠標移出事件
js調用css的方法:
1、利用對象的style屬性:對於中間有橫槓的屬性,需要去掉-將首字母大寫 background-color ->backgroudColor
2、利用className連接到類選擇器。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠標懸停及移出事件</title>
<style type="text/css">
.one{
color:red;
border:6px solid green;
cursor:pointer;
}
</style>
</head>
<script type="text/javascript">
<!--
function fun(){
var p = document.getElementById("p");
//p.style.color = "red";
//p.style.border = "1px solid green";
p.className = "one";
//p.style.backgroundImage="url(images/1.jpg)";
p.style.backgroundColor="red";
}
function fun1(){
var p = document.getElementById("p");
p.className = ""; //選擇器空,去除類選擇器的樣式
p.style.cssText=""; //去除具體的樣式
//p.style.backgroundImage=""; //去除圖片
}
//-->
</script>
<body>
<p οnmοuseοver="fun()" οnmοuseοut="fun1()" id="p">你好</p>
</body>
</html>
b、鼠標點擊事件
onclick
調用css利用style屬性,調用html則可以利用innerHTML!!!
html屬於初始化,js動態修改其屬性!!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠標的單擊事件</title>
<style type="text/css">
img{
width:300px;
height:300px;
}
</style>
</head>
<script type="text/javascript">
<!--
var hb=0;
var test=0;
function init(){
var btn = document.getElementById("btn");
var btn1 = document.getElementById("btn1");
var img = document.getElementById("i");
btn.οnclick=function (){
img.style.display="none";
this.disabled="disabled";
};
btn1.οnclick=function(){
img.style.display="block";
btn.removeAttribute("disabled");
}
}
function fun(obj){
if(hb%2==0)
obj.value="小白";
else
obj.value="小黑";
hb++;
}
function fun1(){
var p = document.getElementById("p");
p.innerHTML="<img src='images/2.gif' id='i' /><br />";
}
function fun2(){
var p = document.getElementById("btn2");
//p.removeAttribute("onclick");
if(test==0)
p.οnclick=function(){};
else
fun(p);
test++;
}
//-->
</script>
<body οnlοad="init()">
<img src="images/1.jpg" id="i" /><br />
<input type="button" value="圖片不見了" id="btn" /><br />
<input type="button" value="圖片出見了" id="btn1" /><br />
<input type="button" value="搜狐" id="btn2" οnclick="fun(this)" /><br />
<input type="button" value="取消搜狐的單擊事件" id="btn4" οnclick="fun2()" /><br />
<input type="button" value="添加一幅圖片" id="btn3" οnclick="fun1()"/><br />
<p id="p"></p>
</body>
</html>
c、加載與卸載事件
onload:
body中,或一張圖片,內容加載完畢後調用
可以調用多個函數,用逗號隔開:οnlοad="fun(),fun1()"
onunload:
關閉頁面。
d、聚焦與離焦事件
onfocus, onblur
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>聚焦離焦事件</title>
</head>
<script type="text/javascript">
<!--
function fun1(obj){
obj.style.backgroundColor="red";
}
function fun2(obj){
obj.style.backgroundColor="";
if(obj.value=="")
{
alert("內容不能爲空!");
//obj.focus();
}
}
//-->
</script>
<body>
名字:<input type="text" οnfοcus="fun1(this)" οnblur="fun2(this)" />
</body>
</html>
e、鍵盤事件
onkeypress,onkeyup,onkeydown
onkeypress: 按下鍵盤按鍵並鬆開
onkeydown : 按下按鍵發生
onkeyup: 鬆開按鍵
<script type="text/javascript">
<!--
function fun(obj,e){
obj.value=e.keyCode;
}
//-->
</script>
<input type="text" οnkeypress="fun(this,event)" />
f、提交與重置事件
onsubmit,onreset
針對於表單,因此必須寫在form表單中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>提交與重置事件</title>
</head>
<script type="text/javascript">
<!--
function check(obj){
var txt = obj.username;
if(txt.value == "")
{
var formation = document.getElementById("p");
formation.innerHTML="<font color='red'>* 用戶名必須填寫</font>";
txt.focus();
return false;
}
return true;
}
function fun(obj){
alert("進來了");
return false;
}
//-->
</script>
<body>
<form method="post" action="01-JavaScript與HTML結合.html" οnsubmit="return check(this)" οnreset="return fun(this)">
用戶名:<input type="text" name="username" /><span id="p"><font color="red">*</font></span><br />
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">g、選擇與改變事件
onselect: 只能用於輸入框. 當選擇輸入框中的文本時發生
onchange: 用於select和文本框.
對於下拉框是在選項發生變化的時候發生
對於文本框是在文本框的內容發生變化並且失去焦點時發生
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>選擇與改變事件</title>
</head>
<script type="text/javascript">
<!--
function fun(object){
alert(object.value);
}
function fun1(v){
alert(v);
}
function fun2(obj,x,y){
obj.options[y].innerText="ll";
}
//-->
</script>
<body>
<input type="text" οnselect="fun(this)" οnchange="fun1(this.value)" /><br />
<select οnchange="fun2(this,this.value,this.selectedIndex)">
<option value="china">中國</option>
<option value="japan">日本</option>
<option value="amarica">美國</option>
</select>
</body>
</html>
去其他頁面的方法:
1、超鏈接
2、window.open
3、refresh
4、表單提交