这是我的问题。下面的代码是一个简化版本,指出了奇怪的错误。
<html>
<head>
<script type = "text/javascript">
window.onload = function test()
{
body = document.getElementsByTagName('body')[0];
div = document.createElement('div');
div.id = 'div';
body.appendChild(div);
document.getElementById('div').innerHTML = "text";
if(document.getElementById('div').childNodes[0] == "text")
{
alert('true');
}else {
alert('false');
}
}
</script>
<style>
</style>
</head>
<body>
</body>
为什么这是假的??它几乎是SAME EXACT字符串。是吗?
.innerHTML
部分?任何与此问题相关的答案都会有所帮助。这是深夜,我生气和困惑。
答案 0 :(得分:1)
您可以比较您的子节点的nodeValue
,因为childNodes
会返回一个对象:
if (document.getElementById('div').childNodes[0].nodeValue == "text")