js属性节点获取和移除
七娃博客835人阅读
关于节点类型不再重复解释,不懂可以看《js节点都有哪些类型?怎么判断是哪种节点类型?》
公共html代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="bb"> <div class="box1"></div> <div class="box"> <p title="hhhh">第1</p> <p dataId="2">第2</p> <p>第3</p> <p>第4</p> </div> <div class="box2"></div> </div> </body> </html>
1.属性节点获取
第一种方法就是直接获取html自带的属性例如:alt,title,href,placeholder... 注意:不用用来获取自定义属性
console.log(pp.children[0].title); //hhhh console.log(pp.children[1].dataId); //undefined
第二种方法 getAttribute()获取
console.log(pp.children[0].getAttribute('title')); //hhhh console.log(pp.children[1].getAttribute('dataId')); //2
属性节点删除:removeAttribute()
pp.children[1].removeAttribute('dataId'); //删除节点属性 console.log(pp.children[1].getAttribute('dataId')); //null
[…] 了解本篇的基础必须知道什么是节点,关于html dom节点知识点和节点类型的知识,分别看《js节点都有哪些类型?怎么判断是哪种节点类型?》和《js属性节点获取和移除》,下面直接进入正题:js中parentNode和parentElement的区别和用法! […]