JavaScript操作BOM對象

2022-12-27 11:21:41 來源:51CTO博客


(相關資料圖)

BOM:瀏覽器對象,用戶能夠利用瀏覽器對象和瀏覽器進行交互window對象  ==>>history對象、document對象、loaction對象window對象的常用屬性和方法:   window.location       獲取地址欄 注意:屬性可以使用=進行賦值   window.history        獲取歷史對象   window.alert()        彈窗   window.prompt()       輸入框   window.confirm()      帶有確認和取消的提示框   window.open()         打開一個新的窗口   window.close()        關閉當前窗口   window.setTimeout()   延時函數(shù)   window.setInterval()  定時函數(shù)history對象:   history.back()        后退,加載上一個URL   history.forward()     前進,加載下一個URL   history.go(1)         1為前進,-1為后退,0為當前l(fā)ocation對象:   location.hostname     獲取主機名和端口號   location.host         獲取主機名   location.href         獲取地址欄         例如: location.     修改頁面地址   location.reload()     重新加載,刷新   location.replace()    替換URLdocument對象:   document.referrer     獲取上一個頁面的URL,一般需要從別的頁面連接進入才能獲取   document.URL                        獲取頁面的URL地址欄   document.getElementById("")         根據(jù)id獲取元素,獲取到一個元素對象   document.getElementsByName("")      根據(jù)name屬性獲取元素,獲取到一個元素對象數(shù)組,需要通過下標依次從上至下取值。              例如:   var names= document.getElementsByName("n1");                       根據(jù)name屬性并且值為n1的獲取元素數(shù)組                取值: 通過下標取值  names[0].innerHTML     document.getElementsByTagName("")   根據(jù)頁面標簽獲取元素,獲取到一個元素對象數(shù)組,需要根據(jù)下標依次從上至下取值。              例如:   var ps=document.getElementsByTagName("p")                       根據(jù)標簽并且標簽為p標簽獲取元素數(shù)組                       取值:通過下標取值   ps[0].innerHTML   write()                             在頁面上輸出內(nèi)容。   innerHTML                           獲取標簽內(nèi)容,可以進行賦值修改。    例如: document.getElementById("p1").innerHTML="";    Date對象:  1、創(chuàng)建一個Date對象              var times=new Date();        2、調(diào)用方法獲取數(shù)據(jù)        times.getDate()        獲得一個月中的幾號        times.getDay()         獲得一個星期的第幾天        times.getHours()       獲得小時        times.getMinutes()     獲得分鐘        times.getSeconds()     獲得秒數(shù)        times.getMonth()       獲取月份是從12月份開始計算   中國的標準月份+1        times.getFullYear()    獲得年份        times.getTime()        獲得時間戳   從1970年1月1日開始計算毫秒Math對象(數(shù)學對象):         Math.ceil()     向上取整數(shù)   例如:  Math.ceil(9.1)    結(jié)果為10           Math.floor()    向下取整數(shù)   例如:  Math.floor(9.1)   結(jié)果為9           Math.round()    四舍五入     例如:  Math.round(3.5)   結(jié)果為4         Math.random()   隨機數(shù) 范圍(0~1) 例如:Math.random()*10   0<結(jié)果<10  例如利用隨機數(shù)取1~10:  Math.floor(Math.random()*10+1)  時間函數(shù):        setTimeout(調(diào)用的函數(shù),等待時間)     延時函數(shù)   等待多少秒執(zhí)行    執(zhí)行一次           例如: setTimeout(function(){    //執(zhí)行的代碼    },1000);                   1000毫秒=1秒  setInterval(調(diào)用的函數(shù),定時時間)    定時函數(shù)   每隔多少秒執(zhí)行    無數(shù)次           例如: setInterval(function(){    //執(zhí)行的代碼    },1000);                   1000毫秒=1秒

標簽: 從上至下 一個新的 向下取整

上一篇:環(huán)球快看:【算法實踐】手把手帶你簡單實現(xiàn)希爾排序
下一篇:天天熱點評!【編程實踐】提高工作效率,避免重復且枯燥的操作,利用Python自動發(fā)送郵件