29 JavaScript之BOM基礎

一、Bom基礎:
打開、關閉窗口 window.open("url");
open
藍色理想運行代碼功能 補充document.write
document.write("abc");在頁面上寫下abc
放在時間裏用:會先清空再寫
平時使用document ,應該是window.document.xxx平時省略寫
open(url,self/-blank/)返回來是新打開的窗口window對象
close、
簡易的運行代碼案例:
<!DOCTYPE HTML>
<html>
<head>
<title>運行代碼效果</title>
<style>
#txt1{
width:200px;
height:200px;
background:#ccc;
}
</style>
<script>
window.οnlοad=function()
{
var oBtn =document.getElementById("btn1");
var oText=document.getElementById("txt1");
oBtn.οnclick=function()
{
//彈出空白頁
var window1= window.open('about:blank','_blank');
window1.document.write(oText.value);
}
};
</script>
</head>
<body>
<textarea id="txt1"></textarea>
<input id="btn1" type="button" value="運行"></input>
<div>
</div>
</body>
</html>

打開關閉窗口的案例:
<input type="button" value="打開窗口"
οnclick="window.open('http.www.baidu.com')"
/>
<input type="button" value="關閉窗口" οnclick="window.close();"/>
但是使用window.close()時候有些問題:
在火狐下不允許腳本關閉由用戶打開的窗口,只能關閉由程序員用open打開的窗口
常用屬性
window.navigator.userAgent 指的是當前瀏覽器的版本信息 判斷瀏覽器類型
window.location 當前網頁的地址 還可以賦值 location的使用很多
二、尺寸和座標
可視區頂部/左部距離網頁頂部/左部距離
scrollTop=document.documentElement.scrollTop
scrollLeft=document.documentElement.scrollLeft
但是chrom兼容寫法應該是document.body.scrollTop/////document.body.scrollLeft
可視區的尺寸:
document.documentElement.clientHeight:可視區高度
document.documentElement.clientWidth 這就是可視區的寬度
固定定位案例:
#div1{
height:100px;
width:100px;
background:red;
position:fixed;
right:0px;
bottom:0px;
}這個固定定位在IE6中不兼容
解決方案:

紅色浮動塊的位置定位:由上圖可知,垂直上的位置公式:
clientHeight-offsetHeight+scrollTop
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章