未捕获的类型错误:无法读取属性'style'为null
Uncaught TypeError: Cannot read property 'style' of null
我有一个从XML文件中读取协调的函数,当它在窗口中松散时,该函数可以很好地工作。但当我把它放在函数中时,就会出现错误。第127行的CCD_ 1。
第127行是
document.getElementById(id).style.top=Ynod;
有什么想法吗?
var main = (function () {
var id=Math.floor((Math.random()*1000000)+1);
document.getElementById("add-new-sticker-btn").addEventListener("click", function(){
id += 1;
console.log(id);
add_sticker(""); /**"" removes undefined "*/
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","/project2/php/insert.php",true); /*Search way... JS to javaScript*/
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("textBoxID="+id);
});
function run() {
readXml();
}
function readXml() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", 'xml/stickers.xml', false);
xmlhttp.send();
var myXML = xmlhttp.responseXML;
stickers = myXML.getElementsByTagName("sticker");
for (i = 0; i < stickers.length; i++) {
var idNod = (stickers[i].getElementsByTagName("id")[0].childNodes[0].nodeValue); /*Get the ID*/
var id = idNod;
var textNod = (stickers[i].getElementsByTagName("text")[0].childNodes[0].nodeValue); /* Text*/
add_sticker(textNod); /*Call creator function*/
var Xnod = (stickers[i].getElementsByTagName("x")[0].childNodes[0].nodeValue) + 'px'; /*Get the x position Add PX for pixel*/
var Ynod = (stickers[i].getElementsByTagName("y")[0].childNodes[0].nodeValue) + 'px'; /*Get the y position*/
var Znod = (stickers[i].getElementsByTagName("z")[0].childNodes[0].nodeValue); /*Get the y position*/
console.log(Ynod) // Gives correct Y and X
console.log(Xnod)
document.getElementById(id).style.top = Ynod;
document.getElementById(id).style.left = Xnod;
document.getElementById(id).style.zIndex = Znod;
}
}
window.addEventListener("load", run);
return {};
}());
idNod未定义,请确保贴纸中有元素,并尝试将贴纸[i].getElementsByTagName("id"([0].childNodes打印到控制台。
Okej我自己发现了这个问题。我调用了一个全局变量var id = idNod;
如果我将var id = idNod;
更改为仅id=idNod
一切正常:(
相关文章:
- 为什么我可以'不要使用xx[this.index].style.display?它's说“;无法读取属性
- 如何让firefox使用style['attribute name']=设置样式属性
- "无法获取属性'的值;style'"当试图在IE中更改样式时
- 如何使主体采用等同于 style=“background-color: returnBlue()” 的样式属性,其中 r
- javascript无法读取属性'style'为null
- 滚动字幕未捕获类型错误:无法读取属性'style'的未定义
- 如何从javascript访问html的STYLE标记中的class属性
- 如何在IE8上使用setAttribute而不是使用style.cssText来更改样式属性
- 无法在Chrome的Javascript控制台中读取null的属性“style”
- 如何使用 .querySelectorAll 选择器定位 .style 属性
- 为包含 HTML 标记的字符串中的元素设置 Style 属性
- JavaScript TypeError:无法读取 null 的属性“style”
- DOM “style” 属性与实际 CSS 样式之间的关系
- document.getElementsByTagName:无法读取未定义的属性“style”
- 未捕获的类型错误:无法读取属性'style'为null
- 是否可以通过nightwatch.js设置元素的Style属性的值?如果是,那么如何
- 未捕获类型错误:无法读取属性'style'null"
- 不能读取属性'style'动态HTML代码中的null
- JS 类型错误:无法读取 null 的属性“style”
- 0x800a138f - JavaScript运行时错误:无法获取属性'style'未定义或空引用的