DOM和BOM基本知识总结
什么是DOM:文档对象模型
什么是BOM:浏览器对象模型。
BOM常见对象模型:
window 代表整个浏览器窗口。window是BOM中的顶级对象,也称全局对象。
navigator 代表浏览器客户端的信息,通过navigator我们可以判断当前用户的浏览器相关信息。
location 代表浏览器地址栏的信息,通过location可以设置或者获取当前url信息
history 代表浏览器的历史访问信息,通过history可以实现刷新、上一页、下一页等。
screen 代表用户当前屏幕的相关信息。
window:
属性:window.innnerWidth 当前浏览器窗口的宽度 window.innerHeight 当前浏览器窗口的高度。
方法:window.open("url","_self"),在当前窗口打开
window.open("url","_blank")在一个新窗口打开
window.close() 关闭窗口
history:
window.history.go(1)前进、 window.history.go(-1)后退、window.history.go(0)刷新
location:
属性:herf 完整的url地址 protocal 协议 host 主机名+端口号 hostname 主机名 port 端口号 pathname相对路径 hash 锚点地址 search? 查询字符串
方法:window.location.reload()强制刷新、window.location.href(url)在当前窗口打开url,可以后退、 window.locaiton.replace(url)当当前窗口打开,不能后退
Screen:
screen.width 获取屏幕的宽度 screen.height 获取屏幕的高度
DOM:
查找:
document.getElementById() 、document.getElementByTagName()、document.getElementByClassName()
节点操作:
element.appendChild()、element.removeChild()、
节点:
element.firstChild、 element.lastChild、element.childNodes、element.parentNode
创建DOM:
Document.creatAttribute()创建属性节点
document.creatElement() 创建元素节点
document.creatTextNode()创建文本节点
DOM属性:
Document.getElementById().innnerHTML 创建html内容
docuemnt.getElementById().style.clor() 改变html样式
h1.setAttribute()增加属性 h1.getAttribute() 获取属性 input.removeAttribute 移除属性