为什么当获得nodeValue时,即使它显示正确,null也会回来
Why does null keep coming back when getting nodeValue even though it displays properly?
我快疯了。我通过Ajax将代码插入页面。插入代码后,我运行一个函数来抓取其中一个div的文本,并将其显示在页面的另一个位置。
有人能告诉我为什么我一直得到一个JavaScript错误说currentText
是空的,即使文本在页面的其他位置正确显示??
var currentText = document.getElementById("current-text"),
updatedTextHere = document.getElementById("updated-text-here");
updatedTextHere.innerHTML = currentText.firstChild.nodeValue;
这是它获取文本值的代码,它通过Ajax插入到主页中:
<div id="current-text" class="hide">January 1, 2011</div>
更新:是这样的:
getDate = function () {
var currentText = document.getElementById("current-text"),
updatedTextHere = document.getElementById("updated-text-here");
updatedTextHere.innerHTML = currentText.firstChild.nodeValue;
},
htmlready = function () {
myDiv.innerHTML = xmlRequest.responseText;
getDate();
},
您遇到的问题是firstChild
不是currentText
的节点。你的nodeValue
没有值,因为它不存在。您应该使用innerHTML
或textContent
。
改变…
updatedTextHere.innerHTML = currentText.firstChild.textContent; // or innerHTML
相关文章:
- php代码在textbox更改事件上显示null
- javascript对象显示为null,但object.properties返回数据
- 我的localStorage结果显示了[null,null,“something”]],而不是“”;什么”;
- 如何在网格视图中显示NULL值
- 为什么我的 webGL 上下文返回为 null,但也显示它已被检索
- 序列化的 JSON 对象在控制器处显示为 Null
- 某些 JSON 字段在命中 MVC 控制器方法时显示为 NULL
- app.js显示错误:TypeError:无法调用方法'查询'为null
- 如果字段为null,则在sitecore中显示none
- Dimplejs Bug.当序列不为null时,行不会显示
- 显示null的Javascript对象
- 如何正确使用熨斗可选?像铁页一样使用时,项目显示为null
- 如果集合在MeteorJS中为null或不为null,则显示模板
- TypeError:document.getElementById(..)为null,但不会停止显示
- 使用jquery或javascript显示null cookie值为空白空间
- getElementById()总是显示NULL状态
- Javascript变量未解析,显示为空白或null
- 当显示的内容在数据库中为NULL值时隐藏DIV
- IE TextField显示"null"对于空值
- 容器有显示null值的ChildNodes().为什么