问题代码是这样的:
WinJS.UI.Pages.define("/pages/home/home.html", {
ready: function (element, options) {
document.getElementById("inhalt").innerHTML = "test"; // causes NullpointerException
}
});但是当我这样做的时候,没有问题。但我不想每次都等3秒。
WinJS.UI.Pages.define("/pages/home/home.html", {
ready: function (element, options) {
window.setTimeout(function() { document.getElementById("inhalt").innerHTML = "test"; }, 3000);
}
});发布于 2014-05-17 07:23:21
这可能是因为ready()是在页面作为DOM的父级之前调用的,所以document.getElementById找不到它。向您传递的是ready函数中的根元素,因此您可以这样做:
element.querySelector('#inhalt').innerHTML = "test";这应该是可行的。页面的最佳实践是不在页面内部使用ids,因此只需将其更改为类class="inhalt"并将其设置为element.querySelector('.inhalt')即可。
https://stackoverflow.com/questions/23693253
复制相似问题