Dom操作
html是xml的一种特殊结构
dom :浏览器把拿到的html代码,结构化为一个浏览器能够识别并且js可操作的一个模型
①获取DOM 节点
var div1= document. getElementById ( ‘div1’) ;
var divList= document. getElementsByTagName ( ‘div’) ;
console. log ( divList. length)
console. log ( divList[ 0 ] )
var containerList= document. getElementsByClassName ( “. contaner”)
var pList= document. querySelectorAll ( ‘p’)
②property
var pList= document. querySelectorAll ( ‘all’) ;
var p= pList[ 0 ] ;
console. log ( p. style. width)
p. style. width= ’100 px’
console. log ( p. className)
p. className= ’p1’
console. log ( p. nodeName)
console. log ( p. nodeType)
新增节点
查询子节点
查询父节点
删除节点
问题1 、dom是哪种基本的数据结构?
树
问题2 、Dom操作的常用API 有哪些?
①获取DOM 节点,以及节点的property和Attribute
②获取父节点,获取子节点 childNodes/ parentNode
③新增节点,删除节点
问题3 、Dom节点的Attribute和Property有和区别?
①property只是一个JS 对象的属性的修改
②Attribute是对html标签属性的修改
BOM 操作
navigator 测试浏览器型号 screen location history
var ua= navigator. userAgent
var isChrome= ua. indexOf ( ‘Chrome’)
console. log ( isChrome)
console. log ( screen. width)
console. log ( scr)
console. log ( location. href)
console. log ( location. protocal)
console. log ( location. pathname)
console. log ( location. search)
console. log ( location. hash)
history. back ( )
histort. forward ( )
事件
通用事件绑定
var btn= document. getElementById ( ‘btn1’) ;
btn. addEventListener ( ‘click’, function ( event) {
console. log ( ‘clicked’)
} )
function bindEvent ( elem, type, fn) {
elem. addEventListener ( type, fn)
}
var a= document. getElementById ( ‘link1’)
bindEvent ( a, ’click’, function ( e) {
e. preventDefault ( )
alert ( ‘clicked’)
} )
注:关于IE 低版本的兼容性
①IE 低版本使用attachEvent绑定事件,和W3C 标准不一样
②IE 低版本使用量以非常少,很多网站都早已不支持
建议对IE 低版本的兼容性:了解即可,无需深究
如果遇到对IE 低版本要求苛刻的面试,果断放弃
事件冒泡 在父级div中定义的事件,会在子级事件执行完之后冒泡上来执行 阻止事件冒泡的方法:e.stopPropagation()
< ! DOCTYPE html>
< html>
< head>
< meta charset= "utf-8" >
< title> < / title>
< / head>
< body>
< div id= "div1" >
< p id= "p1" > 激活< / p>
< p id= "p2" > 取消< / p>
< p id= "p3" > 取消< / p>
< p id= "p4" > 取消< / p>
< / div>
< div id= "div2" >
< p id= "p5" > 取消< / p>
< p id= "p6" > 取消< / p>
< / div>
< script type= "text/javascript" >
function bindEvent ( elem, type, func) {
elem. addEventListener ( type, func)
}
var p1 = document. getElementById ( 'p1' )
bindEvent ( p1, 'click' , function ( e) {
e. stopPropagation ( )
alert ( '激活' )
} )
bindEvent ( body, 'click' , function ( e) {
alert ( '取消' )
} )
function addE ( ) {
var elem = document. getElementById ( 'div1' )
elem. addEventListener ( 'click' , function ( ) {
console. log ( 'div1 by click ----- ' )
} )
var elemChild = elem. children
console. log ( elem)
for ( var i= 0 ; i< elemChild. length; i++ ) {
var iChild = elemChild[ i]
iChild. index = i;
iChild. onclick = ( function ( i) {
return function ( e) {
console. log ( e) ;
console. log ( '第 ' + i+ ' 个div被点击' )
}
} ) ( i)
}
}
< / script>
< / body>
< / html>
< ! DOCTYPE html>
< html>
< head>
< meta charset= "utf-8" >
< title> < / title>
< / head>
< body>
< div id= "div1" >
< a href= "#" > a1< / a>
< a href= "#" > a2< / a>
< a href= "#" > a3< / a>
< a href= "#" > a4< / a>
< a href= "#" > a5< / a>
< p> fjdk< / p>
< h1> jfkd< / h1>
< ! -- ... 随时会新增更多的 a 标签 -- >
< / div>
< script type= "text/javascript" >
var div = document. getElementById ( 'div1' )
function bindEvent ( elem, type, func) {
elem. addEventListener ( type, func)
}
bindEvent ( div, 'click' , function ( e) {
console. log ( e)
console. log ( e. target)
console. log ( e. target. nodeName) ;
if ( e. target. nodeName === 'A' ) {
alert ( e. target. innerHTML)
}
} )
< / script>
< / body>
< / html>