元素节点不是属性节点的父节点,元素节点和元素包含的内容有区别,一个包含文本节点,一个包含文本...

元素节点之间才有父子节点的关系?

属性节点只是包含的关系?不是父子关系。因为属性每个人都有,

节点类型不同,不能有父子关系
狗狗不能生猫猫
另外属性节点当然不能出现在dom树里
任意一个元素节点都有几十上百个属性节点

<div id="foo" class="xxx"></div>hello<p id="foo2"></p><!-- comment -->
<script>
    //元素节点 nodeType = 1
    console.log(document.getElementById('foo').nodeType); 
    //属性节点 nodeType = 2
    console.log(document.getElementById('foo').attributes['id'].nodeType);
    //文本节点 nodeType = 3
    console.log(document.getElementById('foo').nextSibling.nodeType);
    //注释节点 nodeType = 8
    console.log(document.getElementById('foo2').nextSibling.nodeType);
    //文档节点 nodeType = 9
    console.log(document.nodeType);
    //fragment节点 nodeType = 11
    console.log((document.createDocumentFragment()).nodeType);
</script>

2. nodeValue

对于元素节点,因为本身不直接包含文本,所以nodeValue是不可用的。当然你也可以在示例中自己写试试看有什么结果出现。

对于文本节点,nodeValue=文本值

对于属性节点,nodeValue=属性值

转载于:https://www.cnblogs.com/xuedexin/articles/5604353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值