[英]innerHTML strings aren't equal to the same string in javascript?
這是我的問題。 下面的代碼是一個簡化版本,用於指出奇怪的錯誤。
<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>
為什么是假的!? 它實際上是相同的確切字符串。 它是.innerHTML
部分嗎? 任何與此問題相關的答案都會有所幫助。 夜深了,我又生氣又困惑。
您可以比較子節點的nodeValue
,因為childNodes
返回一個對象:
if (document.getElementById('div').childNodes[0].nodeValue == "text")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.