- HTML 教程
- HTML - 首页
- HTML - 路线图
- HTML - 简介
- HTML - 历史与发展
- HTML - 编辑器
- HTML - 基本标签
- HTML - 元素
- HTML - 属性
- HTML - 标题
- HTML - 段落
- HTML - 字体
- HTML - 块
- HTML - 样式表
- HTML - 格式化
- HTML - 引用
- HTML - 注释
- HTML - 颜色
- HTML - 图片
- HTML - 图片地图
- HTML - Iframes
- HTML - 短语元素
- HTML - 元标签
- HTML - 类
- HTML - ID
- HTML - 背景
- HTML 表格
- HTML - 表格
- HTML - 表头与标题
- HTML - 表格样式
- HTML - 表格 Colgroup
- HTML - 嵌套表格
- HTML 列表
- HTML - 列表
- HTML - 无序列表
- HTML - 有序列表
- HTML - 定义列表
- HTML 链接
- HTML - 文本链接
- HTML - 图片链接
- HTML - 邮箱链接
- HTML 颜色名称与值
- HTML - 颜色名称
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML 表单
- HTML - 表单
- HTML - 表单属性
- HTML - 表单控件
- HTML - 输入属性
- HTML 媒体
- HTML - 视频元素
- HTML - 音频元素
- HTML - 嵌入多媒体
- HTML 头部
- HTML - Head 元素
- HTML - 添加 Favicon
- HTML - Javascript
- HTML 布局
- HTML - 布局
- HTML - 布局元素
- HTML - 使用 CSS 进行布局
- HTML - 响应式设计
- HTML - 符号
- HTML - 表情符号
- HTML - 样式指南
- HTML 图形
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - 拖放 API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web 存储
- HTML - 服务器发送事件
- HTML 其他
- HTML - 文档对象模型 (DOM)
- HTML - MathML
- HTML - 微数据
- HTML - IndexedDB
- HTML - Web 消息传递
- HTML - Web CORS
- HTML - WebRTC
- HTML 演示
- HTML - 音频播放器
- HTML - 视频播放器
- HTML - 网页幻灯片
- HTML 工具
- HTML - Velocity Draw
- HTML - 二维码
- HTML - Modernizer
- HTML - 验证
- HTML - 颜色选择器
- HTML 参考
- HTML - 速查表
- HTML - 标签参考
- HTML - 属性参考
- HTML - 事件参考
- HTML - 字体参考
- HTML - ASCII 码
- ASCII 码表查询
- HTML - 颜色名称
- HTML - 实体
- MIME 媒体类型
- HTML - URL 编码
- 语言 ISO 代码
- HTML - 字符编码
- HTML - 已弃用的标签
- HTML 资源
- HTML - 快速指南
- HTML - 有用资源
- HTML - 颜色代码生成器
- HTML - 在线编辑器
HTML - DOM 元素 normalize() 方法
normalize() 方法用于通过删除任何空文本节点来组合 HTML 元素内的相邻文本节点。
语法
node.normalize();
返回值
此方法不返回任何值。
HTML DOM 元素“normalize()”方法示例
以下是 normalize() 方法的一些示例,演示了它如何合并 HTML DOM 元素内的相邻文本节点。
合并相邻文本节点
此示例向我们展示了 normalize() 方法如何通过删除多余的空格来组合嵌套在<div>元素内的相邻文本节点。
<!DOCTYPE html>
<html lang="en">
<head>
<title>normalize() Method Example</title>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>normalize() Method</h2>
<p>Combines the adjacent text nodes and
removing extra whitespace.
</p>
<div id="pt">
<p>This is <em>some</em> text nodes</p>
<div>-----spaces----</div>
<p>Another text node with spaces</p>
<div>---spaces----and a;lll</div>
<p>And <em>more </em> text nodes</p>
</div>
<div id="output"></div>
<div style="margin-top: 20px;">
<button onclick="normalizeAndDisplay()">
Normalize Content
</button>
</div>
<script>
function normalizeAndDisplay() {
const pDiv = document.getElementById('pt');
pDiv.normalize();
const outputDiv=document.getElementById('ot');
outputDiv.innerHTML =
`<p><strong>Normalized HTML:</strong></p>
${pDiv.innerHTML}`;
// Remove spacer elements from output
const spacerElements =
outputDiv.querySelectorAll('div');
spacerElements.forEach(element => {
element.remove();
});
}
</script>
</body>
</html>
动态合并和规范化元素
此示例演示了 normalize() 方法的动态用法。单击按钮后,它将组合并规范化 HTML 元素的内容。
<!DOCTYPE html>
<html lang="en">
<head>
<title>normalize() Method Example</title>
</head>
<body>
<h1>The Element Object</h1>
<h2>The normalize() Method</h2>
<p>Add Text Normalize</p>
<p id="paragraph">Click one button to add text to this
paragraph, click the other button to normalize the paragraph.
</p>
<div class="button-container">
<button onclick="addText()">Add Text</button>
<button onclick="normalizeParagraph()">
Normalize Paragraph
</button>
</div>
<p id="output">The paragraph above has
<span id="child-count">1</span> child node(s).
</p>
<script>
function addText() {
const paragraph = document.getElementById
('paragraph');
const newText = document.createTextNode
(' Additional text.');
paragraph.appendChild(newText);
updateChildCount();
}
function normalizeParagraph() {
const paragraph = document.getElementById
('paragraph');
paragraph.normalize();
updateChildCount();
}
function updateChildCount() {
const childCount = document.getElementById
('paragraph').childNodes.length;
document.getElementById
('child-count').textContent = childCount;
}
</script>
</body>
</html>
支持的浏览器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| normalize() | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
广告




