急,请资深专家进,javascript节点空格问题?
<body>
<input type=button value=寻找 onclick="alert('--'+document.body.childNodes[1].nodeValue+'--')"/>
<input type=button id="kk" value="hao" onclick="alert(document.getElementById('kk').value)"/>
</body>
首先,我在input和button后面都没有写空格,也加上了/作为封闭,为什么会在这两个input后面都会跟一个文本节点,而且这两个文本都是空格,为什么,我没在input后面写空格啊.第二:
<body>
<input type=button value=寻找 onclick="alert('--'+document.body.childNodes[1].nodeValue+'--')"/>
<input type=button id="kk" value="hao" onclick="alert(document.getElementById('kk').value)"/>
<button>杀杀杀</button>
</body>
为什么这里加了button之后,第一个input后面有#text文本节点,而第二个input后面就没有#text文本节点了?在</button>也有一个#text文本节点,文本节点的nodeValue值也是一个空格,为什么,我没在后面带空格啊?
<body>
<input type=button value=寻找 onclick="alert('--'+document.body.childNodes[1].nodeValue+'--')"/>
<input type=button id="kk" value="hao" onclick="alert(document.getElementById('kk').value)"/>
</body>
首先,我在input和button后面都没有写空格,也加上了/作为封闭,为什么会在这两个input后面都会跟一个文本节点,而且这两个文本都是空格,为什么,我没在input后面写空格啊.第二:
<body>
<input type=button value=寻找 onclick="alert('--'+document.body.childNodes[1].nodeValue+'--')"/>
<input type=button id="kk" value="hao" onclick="alert(document.getElementById('kk').value)"/>
<button>杀杀杀</button>
</body>
为什么这里加了button之后,第一个input后面有#text文本节点,而第二个input后面就没有#text文本节点了?在</button>也有一个#text文本节点,文本节点的nodeValue值也是一个空格,为什么,我没在后面带空格啊?
1.没有空格但是有回车
2.这个还真不清楚。但是button好像并不是合法的HTML元素。你换个其他的试试。
装个看IE DOM TREE的插件你会看见.
就算你INPUT后面直接跟</BODY>
IE解释后也会凭空搞个空文本节点出来的.
<input type=button value=寻找 onclick="alert('--'+document.body.childNodes[1].nodeValue+'--')"/>
<input type=button id="kk" value="hao" onclick="alert(document.getElementById('kk').value)"/>
<button>杀杀杀 </button>
</body>那第二个input后面为什么就不造个空文本节点出来?
<div id="test" style="border:1px solid buttonface;">
<p>p1 test</p> <p>test 2</p>
<p>p2 test</p>
</div><script type="text/javascript">
function resetChildNodes(_elm){
var _cn = _elm.childNodes,_nd = null;
for(var i=0;i<_cn.length;i++)
_nd = _cn[i];
if(_nd.nodeType==3&&!(_nd.nodeValue.replace(/\s/g,''))){
_elm.removeChild(_nd);
}
}
}
var obj = document.getElementById("test");resetChildNodes(obj);
document.write('<p>test childNodes[iIndex]:<br/>');
document.write(obj.childNodes[0].innerHTML+'<br/>');
document.write(obj.childNodes[1].innerHTML+'<br/>');
document.write(obj.childNodes[2].innerHTML+'<br/>');
document.write('</p>');document.write('<p>test firstChild & lastChild:<br/>');
document.write(obj.firstChild.innerHTML+'<br/>');
document.write(obj.lastChild.innerHTML+'<br/>');
document.write('</p>');
</script>
不过具体生成空文本节点的规则还是搞不懂.