JavaScript getElementById() 用法:按 ID 查找元素
document.getElementById("notice") 查找 ID 恰好为 notice 的元素,区分大小写,找不到时返回 null。传入 ID 本身,无需 CSS 选择器使用的 #。
使用准确的 ID 查找元素
const notice = document.getElementById("notice");
if (notice !== null) {
notice.textContent = "처리가 완료되었습니다.";
}
getElementById() 返回 DOM(文档对象模型)中的元素。读取文字可用 textContent。方法名末尾为 Id。普通父元素上没有这个方法。
查找、修改和恢复的完整示例
把示例以 UTF-8 保存为 get-element-by-id.html 后打开。无需额外库。脚本放在按钮和输出元素之后。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Find an element by ID</title>
</head>
<body>
<h1>Find an element by ID</h1>
<p id="notice">Initial notice</p>
<label for="lookup-id">ID to find</label>
<select id="lookup-id">
<option value="notice">notice</option>
<option value="Notice">Notice</option>
<option value="#notice">#notice</option>
<option value="missing">missing</option>
</select>
<button id="find" type="button">Find</button>
<button id="change" type="button">Change notice</button>
<button id="restore" type="button">Restore</button>
<pre id="output"></pre>
<script>
const notice = document.getElementById("notice");
const selectedId = document.getElementById("lookup-id");
const output = document.getElementById("output");
function findElement() {
const element = document.getElementById(selectedId.value);
if (element === null) {
output.textContent = "Element not found.";
return;
}
output.textContent = `Tag: ${element.tagName}\nText: ${element.textContent}`;
}
document.getElementById("find").addEventListener("click", findElement);
document.getElementById("change").addEventListener("click", () => {
notice.textContent = "Updated notice";
findElement();
});
document.getElementById("restore").addEventListener("click", () => {
notice.textContent = "Initial notice";
selectedId.value = "notice";
findElement();
});
findElement();
</script>
</body>
</html>
开始显示 Tag: P 和 Text: Initial notice。Change notice 修改同一元素。选择 Notice 或 #notice 后按 Find,会显示未找到。Restore 恢复文字和选项。如果得到 null,函数会提前返回,不读取元素属性。
排查有元素却返回 null 的原因
先核对实际 id、大小写和空格。仅设置 name 或 class 无法匹配。如果脚本执行过早,把脚本放在元素后面,或给外部经典 script 添加 defer。内联脚本不会这样延迟。createElement() 创建的元素也要插入文档后才能从文档找到。
<script src="app.js" defer></script>
重复 ID 和其他选择方法
保持 ID 唯一。重复时返回树顺序中的第一个。重复卡片可用类。querySelector() 返回第一个 CSS 匹配,querySelectorAll() 返回所有匹配。修改查找到的元素时可参考 innerText, innerHTML, textContent。
const one = document.querySelector("#notice");
const cards = document.querySelectorAll(".card");
在文档片段内查找含冒号的 ID
在示例页的 Console 中运行代码。直接按 ID 查找时传入 result:2026。构造 CSS 选择器时,先用 CSS.escape() 转义 ID,再加上 #,因为冒号在选择器中有特殊含义。
{
const fragment = document.createDocumentFragment();
const title = document.createElement("p");
title.id = "result:2026";
fragment.append(title);
console.log(fragment.getElementById(title.id) === title); // true
console.log(fragment.querySelector("#" + CSS.escape(title.id)) === title); // true
console.log(document.getElementById(title.id) === null); // true in the example
}
三行都输出 true。前两行在 DocumentFragment 内找到元素。片段没有插入文档,因此文档查询找不到。如果已有元素引用,可以直接使用该引用。




