<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<script language="javascript">
var head=document.getElementsByTagName('head')[0];
for(var i=0;i<head.childNodes.length;i++){
alert(head.childNodes[i].nodeName)
}
</script>
</body>
</html>大家在IE和火狐下测试下,看看为什么弹出的内容不一样,火狐下弹出了几个"#text",啥意思啊?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<script language="javascript">
var head=document.getElementsByTagName('head')[0];
for(var i=0;i<head.childNodes.length;i++){
alert(head.childNodes[i].nodeType)
}
</script>
</body>
</html>弹出3的就是空节点
正解,通过nodeType来判断。
ff和IE处理xml有所不同