1. 程式人生 > 其它 >javascript(操作BOM物件)

javascript(操作BOM物件)

javascript與瀏覽器的關係?

javascript誕生就是為了能夠讓他在瀏覽器中執行

BOM:瀏覽器物件模型

·IE 6~11

·Chrome

·Safari

·FireFox

三方

QQ瀏覽器

360瀏覽器

Window

window代表瀏覽器視窗

window.alert(1)

window.innerHeighr

Navigator

Navigator封裝了瀏覽器的資訊

navigator.appName

大多數不會用navigator物件,會被人修改

screen(代表螢幕的尺寸)

screen.width
screen.heigth

location(主要)

location代表當前網頁的URL的資訊

host:

href:

protocol(協議):"https:"

reload:f reload()//重新整理網頁

document

document代表當前的頁面,

document.title

document.title='~~~'

history:代表瀏覽器的歷史記錄

history.back()//後退

history.forward()//前進

操作DOM物件(重點)

核心

瀏覽器網頁就是一個DOM樹形結構

  ·更新:更新dom節點

操作文字

·id1.innerText=’456' 修改文字的值

·id.innerHTML=’<strong>123<strong>'

操作js

id1.style.color=’red‘

id1.style.fontSize=’20px‘//駝峰命名法

·遍歷dom節點:得到Dom節點

·刪除:刪除一個Dom節點

刪除節點的步驟:先獲取父節點,在通過父節點刪除自己

p1.parentElement()//獲取父類節點

·新增:新增Dom節點

建立一個標籤,插入
<script>
var
js =document.getElementById('js'),//已經存在的節點
list=document.getElementById('list');
list.appendChild(js);//追加到後面
//通過JS建立一個新的節點
var newP = document.createElement('p');//建立一個p標籤
newP.id='newP';
newP.innerText='hello';
</script>

要操作一個節點,必須先獲得這個節點

獲得dom節點