HTML DOM 方法:参数、返回值与异常
逐项学习查询、创建和树修改方法,观察返回节点与异常条件。
学完这一章,你能够
- 逐项学习查询、创建和树修改方法,观察返回节点与异常条件。
- 解释对应代码的输入、动作、返回与限制
- 通过本章独立实例观察实际DOM变化
方法与属性的区别
调用方法通常需要括号和参数,例如document.getElementById(id);读取属性则取得当前值,例如element.parentNode。方法也可能只查询而不修改,不能把所有方法都理解为写操作。
getElementById由Document等适用接口提供,并非所有Node都可调用。普通Element中的局部查找可使用querySelector,查询不到时先处理null。
创建后再插入
createElement和createTextNode只创建对象;appendChild才将节点接到父节点末尾。插入已有节点会移动它,不会自动复制。需要独立副本时另行考虑cloneNode。
insertBefore的参照节点必须属于该父节点,传null表示末尾。removeChild返回被移除节点,replaceChild返回旧节点;两者在目标不是直接子节点时会抛出NotFoundError。
属性与对象示例
createAttribute创建Attr对象,通过setAttributeNode附着;常规字符串属性更常用setAttribute和getAttribute。未设置的属性读取结果可能是null,布尔属性应区分存在性与当前IDL属性。
实例覆盖创建、插入、替换和删除的顺序,并故意再次删除已移除节点,捕获实际异常名称。
接口与条件一览
| 接口 | 参数与返回 | 条件或异常 |
| getElementById(id) | 字符串ID;Element或null | Document等适用接口,ID唯一 |
| getElementsByTagName(name) | 标签名;实时HTMLCollection | 查询作用域内的后代,不是Array |
| getElementsByClassName(names) | 空格分隔类名;实时HTMLCollection | 匹配全部指定类名,旧IE存在限制 |
| appendChild(node) | 新子节点;返回该节点 | 已有节点移动,非法层级会失败 |
| removeChild(child) | 直接子节点;返回被删节点 | 不属于该父节点则NotFoundError |
| replaceChild(new,old) | 新、旧节点;返回old | old须为直接子节点 |
| insertBefore(node,reference) | 节点与参照;返回插入节点 | reference为null则末尾,否则须属于父节点 |
| createAttribute(name) | 属性名;返回Attr | 非法名称可能InvalidCharacterError |
| createElement(name) | 标签名;返回Element | 尚未自动插入文档 |
| createTextNode(data) | 文字;返回Text | 不会把文字解析为HTML |
| getAttribute(name) | 属性名;字符串或null | 读取内容属性,不一定是当前控件状态 |
| setAttribute(name,value) | 名称和值;返回undefined | 布尔属性按存在性解释,必要时用IDL属性 |
完整源码与实际运行
展开查看收起
JavaScript
"use strict";
const results=Object.create(null);
const emit=(key,value)=>{results[key]=value===undefined?{type:"undefined"}:value;document.querySelector("#output").textContent=JSON.stringify(results,null,2);};
window.__mbskyResults=results;window.__mbskyErrors=[];
window.addEventListener("error",e=>window.__mbskyErrors.push(e.message));
(async()=>{
const list=document.querySelector('#steps');const old=document.querySelector('#old');
const next=document.createElement('li');next.appendChild(document.createTextNode('理解节点'));
emit('appendReturnsNew',list.appendChild(next)===next);
const first=document.createElement('li');first.textContent='阅读说明';list.insertBefore(first,old);
const replacement=document.createElement('li');replacement.textContent='运行实例';
emit('replaceReturnsOld',list.replaceChild(replacement,old)===old);
emit('removeReturnsRemoved',list.removeChild(first)===first);
const attr=document.createAttribute('data-phase');attr.value='practice';next.setAttributeNode(attr);
emit('attribute',next.getAttribute('data-phase'));next.setAttribute('title','继续学习');
emit('order',Array.from(list.children,x=>x.textContent));
try{list.removeChild(old);}catch(error){emit('missingChildError',error.name);}
})().then(()=>window.__mbskyDone=true).catch(error=>{window.__mbskyErrors.push(String(error));emit("runtimeError",String(error));window.__mbskyDone=true;});
尝试一下 ↗ 在独立页面修改并运行
- {"appendReturnsNew": true, "replaceReturnsOld": true, "removeReturnsRemoved": true, "attribute": "practice", "order": ["运行实例", "理解节点"], "missingChildError": "NotFoundError"}
本章附件与实例包
课程关联工程包(登记关联13章) · 含当前章节或实例 · mbskycom202611a439.zip
常见错误与边界检查
- 没有检查null就直接访问节点属性。
- 把源码存在、属性赋值或示例可打开当成实际教学行为已验收。
- 在file或不具备能力的沙箱内推断HTTP、Cookie和同源行为。
技术规范与官方资料
练习与参考解答
查看参考解答
先保存操作前的DOM或属性值,再执行单一操作并比较输出。查询不到、非法参照、实时集合变化及资源加载失败按对应章节处理,不伪造成功结果。