Javascript JS:无法转换为对象,与子节点相关
好吧,我觉得很愚蠢,但我想知道到底是什么问题 虽然该函数正常工作,但我在Opera中遇到了这个JS错误。不确定其他浏览器 未捕获异常:TypeError:无法 转换 'document.getElementById(“shoutbox\u区域” +moduleId)“”到对象 oElement=document.getElementById(“shoutbox\u区域” +moduleId)子节点 以下是相关代码:Javascript JS:无法转换为对象,与子节点相关,javascript,arrays,children,Javascript,Arrays,Children,好吧,我觉得很愚蠢,但我想知道到底是什么问题 虽然该函数正常工作,但我在Opera中遇到了这个JS错误。不确定其他浏览器 未捕获异常:TypeError:无法 转换 'document.getElementById(“shoutbox\u区域” +moduleId)“”到对象 oElement=document.getElementById(“shoutbox\u区域” +moduleId)子节点 以下是相关代码: function appendShout(XMLDoc) { var shou
function appendShout(XMLDoc)
{
var shoutData = XMLDoc.getElementsByTagName("item");
var oElement = [];
if (shoutData.length > 0)
{
var moduleId = shoutData[0].getAttribute("moduleid");
if (shoutData[shoutData.length - 1].getAttribute("lastshout") != "undefined")
{
for (var i = 0; i < shoutData.length; i++)
if (shoutData[i].firstChild.nodeValue != 0)
document.getElementById("shoutbox_area" + moduleId).innerHTML += shoutData[i].firstChild.nodeValue;
oElement = document.getElementById("shoutbox_area" + moduleId).childNodes;
var i = oElement.length;
while (i--)
{
if (i % 2 == 0)
oElement[i].className = "windowbg2";
else
oElement[i].className = "windowbg";
}
oElement[oElement.length - 2].style.borderBottom = "1px black dashed";
}
}
}
我使用以下函数发送和获取XMLHttpRequest,正如您可能已经在上面的removeShout函数中注意到的那样:
// Load an XML document using XMLHttpRequest.
function getXMLDocument(sUrl, funcCallback)
{
if (!window.XMLHttpRequest)
return null;
var oMyDoc = new XMLHttpRequest();
var bAsync = typeof(funcCallback) != 'undefined';
var oCaller = this;
if (bAsync)
{
oMyDoc.onreadystatechange = function () {
if (oMyDoc.readyState != 4)
return;
if (oMyDoc.responseXML != null && oMyDoc.status == 200)
{
if (funcCallback.call)
{
funcCallback.call(oCaller, oMyDoc.responseXML);
}
// A primitive substitute for the call method to support IE 5.0.
else
{
oCaller.tmpMethod = funcCallback;
oCaller.tmpMethod(oMyDoc.responseXML);
delete oCaller.tmpMethod;
}
}
};
}
oMyDoc.open('GET', sUrl, bAsync);
oMyDoc.send(null);
return oMyDoc;
}
// Send a post form to the server using XMLHttpRequest.
function sendXMLDocument(sUrl, sContent, funcCallback)
{
if (!window.XMLHttpRequest)
return false;
var oSendDoc = new window.XMLHttpRequest();
var oCaller = this;
if (typeof(funcCallback) != 'undefined')
{
oSendDoc.onreadystatechange = function () {
if (oSendDoc.readyState != 4)
return;
if (oSendDoc.responseXML != null && oSendDoc.status == 200)
funcCallback.call(oCaller, oSendDoc.responseXML);
else
funcCallback.call(oCaller, false);
};
}
oSendDoc.open('POST', sUrl, true);
if ('setRequestHeader' in oSendDoc)
oSendDoc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
oSendDoc.send(sContent);
return true;
}
希望这已经足够好了,您可以在其上查看源代码以查看实际的HTML,但是有一些属性在运行时添加到Shoutbox标记中,以便与XHTML兼容,等等
如果您还需要什么,请告诉我
谢谢:)代码正在中断,因为这两行中的第二行shoutID为null,第二次通过循环:
var shoutID = document.getElementById(shout.parentNode.id);
var moduleID = shoutID.parentNode.getAttribute("moduleid");
第一行很奇怪。为什么不直接使用var shoutID=shout.parentNode代码>?
而且,moduleId
属性似乎不存在
当循环时,您试图通过实现什么?模块ID
等于什么?目标元素的id是什么?它包含哪些子节点?HTML会很有用。编辑问题。希望这是足够的信息,你现在能够帮助我。如果您还需要什么,请告诉我。非常感谢!我非常感谢你的帮助!:)哇,谢谢。我忽略了那部分代码。我实际上在考虑允许同时删除多个呼喊。无论如何,我删除了那个循环,它似乎已经修复了JS错误中的一个。但是,moduleid
属性是在运行时创建的,因此不确定这是一个问题的原因。。。?非常感谢。天哪,你太棒了,而我是弱智!我将moduleID作为该函数中的第二个参数传递,因此删除了以下内容:var moduleID=shoutID.parentNode.getAttribute(“moduleID”)代码>并从第二个参数中获取正确的moduleId值。谢谢你在这件事上又帮了我一把。但愿我有时有四只眼睛。万分感谢!
var shoutID = document.getElementById(shout.parentNode.id);
var moduleID = shoutID.parentNode.getAttribute("moduleid");