DOM和BOM操作

3,109 阅读3分钟

javacsript是通过访问BOM(Browser Object Model)对象来访问、控制、修改客户端(浏览器),由于BOM的window包含了document,window对象的属性和方法是直接可以使用而且被感知的,因此可以直接使用window对象的document属性,通过document属性就可以访问、检索、修改XHTML文档内容与结构。因为document对象又是DOM(Document Object Model)模型的根节点。可以说,BOM包含了DOM(对象),浏览器提供出来给予访问的是BOM对象,从BOM对象再访问到DOM对象,从而js可以操作浏览器以及浏览器读取到的文档。

DOM

查找

document.getElementById('id')
document.getElementByClassName('classname')
document.getElementsByTagName('tag')
document.querySelector('#foo > div.bar')
document.querySelectorAll('.bar')

节点操作

// Append element1 as the last child of element2
element1.appendChild(element2)

myParentElement.removeChild(myElement)
myElement.parentNode.removeChild(myElement)

replaceChild()

// Create a clone
const myElementClone = myElement.cloneNode()
myParentElement.appendChild(myElementClone)

// Insert element2 as child of element 1, right before element3
element1.insertBefore(element2, element3)
// insert sp1 after sp2
parentDiv.insertBefore(sp1, sp2.nextSibling);

myElement.childNodes
myElement.firstChild
myElement.lastChild
myElement.previousSibling
myElement.nextSibling
myElement.parentNode
myElement.parentElement

创建DOM

document.createAttribute() // 创建属性节点
document.createElement() // 创建元素节点
doucment.createTextNode() // 创建文本节点
document.createComment() // 创建新的注释节点
document.createDocumentFragment() // 创建文档片段节点

DOM属性

document.getElementById("p1").innerHTML="New text!" // 创建 HTML 内容
document.getElementById("p2").style.color="blue" // 改变 HTML 样式
h1.setAttribute('class', 'hello')
h1.getAttribute('class')
input.removeAttribute('checked')

事件监听

myElement.onclick = function onclick (event) {
  console.log(event.type + ' got fired')
}
这是通常应该避免采用的方法。这里,.onclick是一个元素的属性,也就是说你可以修改它,但是你不能用它再绑定其他的监听函数-你只能把新的函数赋给它,覆盖掉旧函数的引用。

myElement.addEventListener('click', function (event) {
  console.log(event.type + ' got fired')
})

BOM

window对象

history对象

history.go(1)
history.back()
history.forward()

document对象

document对象:实际上是window对象的属性,document == window.document为true,是唯一一个既属于BOM又属于DOM的对象 
 
document.lastModified //获取最后一次修改页面的日期的字符串表示 
 
document.referrer //用于跟踪用户从哪里链接过来的 
 
document.title //获取当前页面的标题,可读写 
 
document.URL //获取当前页面的URL,可读写 
 
document.anchors[0]或document.anchors["anchName"] //访问页面中所有的锚 
 
document.forms[0]或document.forms["formName"] //访问页面中所有的表单 
 
document.images[0]或document.images["imgName"] // 访问页面中所有的图像 
 
document.links [0]或document.links["linkName"] //访问页面中所有的链接 
 
document.applets [0]或document.applets["appletName"] //访问页面中所有的Applet 
 
document.embeds [0]或document.embeds["embedName"] //访问页面中所有的嵌入式对象 
 
document.write(); 或document.writeln(); //将字符串插入到调用它们的位置 

location对象

location对象:表示载入窗口的URL,也可用window.location引用它 
 
location.href //当前载入页面的完整URL,如http://www.somewhere.com/pictures/index.htm 
 
location.portocol //URL中使用的协议,即双斜杠之前的部分,如http 
 
location.host //服务器的名字,如www.wrox.com 
 
location.hostname //通常等于host,有时会省略前面的www 
 
location.port //URL声明的请求的端口,默认情况下,大多数URL没有端口信息,如8080 
 
location.pathname //URL中主机名后的部分,如/pictures/index.htm 
 
location.search //执行GET请求的URL中的问号后的部分,又称查询字符串,如?param=xxxx 
 
location.hash //如果URL包含#,返回该符号之后的内容,如#anchor1 
 
location.assign("http:www.baidu.com"); //同location.href,新地址都会被加到浏览器的历史栈中 
 
location.replace("http:www.baidu.com"); //同assign(),但新地址不会被加到浏览器的历史栈中,不能通过back和forward访问 
 
location.reload(true | false); //重新载入当前页面,为false时从浏览器缓存中重载,为true时从服务器端重载,默认为false 

navigator对象

navigator对象:包含大量有关Web浏览器的信息,在检测浏览器及操作系统上非常有用,也可用window.navigator引用它 
 
navigator.appCodeName //浏览器代码名的字符串表示 
 
navigator.appName //官方浏览器名的字符串表示 
 
navigator.appVersion //浏览器版本信息的字符串表示 
 
navigator.cookieEnabled //如果启用cookie返回true,否则返回false 
 
navigator.javaEnabled //如果启用java返回true,否则返回false 
 
navigator.platform //浏览器所在计算机平台的字符串表示 
 
navigator.plugins //安装在浏览器中的插件数组 
 
navigator.taintEnabled //如果启用了数据污点返回true,否则返回false 
 
navigator.userAgent //用户代理头的字符串表示  

screen对象

screen对象:用于获取某些关于用户屏幕的信息,也可用window.screen引用它 
 
screen.width/height //屏幕的宽度与高度,以像素计 
 
screen.availWidth/availHeight //窗口可以使用的屏幕的宽度和高度,以像素计 
 
screen.colorDepth //用户表示颜色的位数,大多数系统采用32位 
 
window.moveTo(0, 0); 
 
window.resizeTo(screen.availWidth, screen.availHeight); //填充用户的屏幕