DOM Node对象normalize()
方法标准化添加所有文本节点,包括定义正常形式的属性节点,其中包含元素,注释,处理指令,CDATA部分和实体引用的节点的结构将文本节点分开,即既不相邻的文本节点也不分隔空文本节点。
语法
以下是normalize()
方法的使用语法。
nodeobject.normalize();
返回值
- 此方法没有参数,也没有返回值。
示例
文件:node.xml 的内容如下 -
<Company>
<Employee category = "Technical" id = "firstelement">
<FirstName>Susen</FirstName>
<LastName>Su</LastName>
<ContactNo>1584567890</ContactNo>
<Email>susen@yiibai.com</Email>
</Employee>
<Employee category = "Non-Technical">
<FirstName>Max</FirstName>
<LastName>Su</LastName>
<ContactNo>1334667898</ContactNo>
<Email>maxsu@yiibai.com</Email>
</Employee>
<Employee category = "Management">
<FirstName>Min</FirstName>
<LastName>Su</LastName>
<ContactNo>1364562350</ContactNo>
<Email>minsu@yiibai.com</Email>
</Employee>
</Company>
以下示例演示了normalize()
方法的用法 -
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
<script>
function loadXMLDoc(filename) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // code for IE5 and IE6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
</script>
</head>
<body>
<script>
xmlDoc = loadXMLDoc("/node.xml");
x = xmlDoc.createElement('Employee');
x.appendChild( document.createTextNode("EmployeeA ") );
x.appendChild( document.createTextNode("EmployeeB ") );
document.write("<b>正规化前</b><br>");
document.write("<b>子节点长度: </b>"+x.childNodes.length+"<br>");
document.write("<b>第一个子节点: </b>"+x.childNodes[0].textContent+"<br>");
document.write("<b>第二个子节点: </b>"+x.childNodes[1].textContent+"<br>");
x.normalize();
document.write("<b>正规化后</b><br>");
document.write("<b>子节点长度: </b>"+x.childNodes.length+"<br>");
document.write("<b>第一个子节点: </b>"+x.childNodes[0].textContent+"<br>");
</script>
</body>
</html>
执行上面示例代码,得到以下结果 -