C# Microsoft JScript运行时错误:无法获取属性';removeChild';:对象为null或未定义
我正在开发一个asp.NETMVC3应用程序。标题告诉我有什么问题。我将描述我是如何得到这个错误的 我正在使用JavaScript从razor视图上传图像。脚本没有那么长,所以我将在这里发布:C# Microsoft JScript运行时错误:无法获取属性';removeChild';:对象为null或未定义,c#,javascript,asp.net-mvc-3,razor,C#,Javascript,Asp.net Mvc 3,Razor,我正在开发一个asp.NETMVC3应用程序。标题告诉我有什么问题。我将描述我是如何得到这个错误的 我正在使用JavaScript从razor视图上传图像。脚本没有那么长,所以我将在这里发布: function fileUpload(form, action_url, div_id) { // Create the iframe... var iframe = document.createElement("iframe"); iframe.set
function fileUpload(form, action_url, div_id) {
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "upload_iframe");
iframe.setAttribute("name", "upload_iframe");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");
// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";
iframeId = document.getElementById("upload_iframe");
// Add event...
var eventHandler = function () {
if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);
// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
document.getElementById(div_id).innerHTML = content;
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}
if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);
// Set properties of form...
form.setAttribute("target", "upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");
// Submit the form...
form.submit();
document.getElementById(div_id).innerHTML = "Uploading...";
form.setAttribute("action", '/forms/displayform');
}
通过使用此脚本,我在控制器中获得了执行业务逻辑的映像,因此我不确定是否是脚本导致了此问题,但在调试时,我找不到其他原因。因此,当控制器完成其工作时,我会在visual studio 2010中打开一个新选项卡,标题为Script block[dynamic]
,其中包含以下代码:
function anonymous()
{
iframeId.parentNode.removeChild(iframeId)
}
这实际上是我的JavaScript的一部分:
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
我对JS有非常基本的了解。我使用这个代码,但我不理解其中的某些部分。这里-通过注释和代码本身可以看出发生了什么,但如果我对其进行注释并尝试上载图像,我不会出现错误,这是一件非常诱人的事情,但我认为最有可能的是代码是正确的,出现此错误的原因是其他地方,所以我在这里发帖是想得到关于什么可能导致这个错误以及如何修复它的帮助
附言
这是我用来上传图像的表格:
@using (Html.BeginForm("Upload", "Forms", FormMethod.Post))
{
<input name=@Model[0].DocumentId type="hidden" />
<input type="file" name="datafile" id="file" onchange="readURL(this);" />
<input type="button" name="Button" value="Upload Image" id="UploadButton" onclick="fileUpload(this.form,'/forms/upload','uploadImg'); return false;"/>
<div id="uploadImg" style="display: inline-block;">
<img id="blah" src="#" alt="your image" style="display:none;"/>
</div>
}
@使用(Html.BeginForm(“Upload”,“Forms”,FormMethod.Post))
{
}
这是setTimeout的语法
setTimeout(function(),milliseconds);
所以,你应该这样做
setTimeout(function(){
iframeId.parentNode.removeChild(iframeId);
}, 250);
我得到了与前面代码完全相同的错误,但至少现在我可以在VS2010中进行某种调试,它显示
parentNode
实际上为空。我不太确定这在整个函数的上下文中意味着什么,但我在想,如果(iframeId!=null){//这里是代码的其余部分;}或者必须有父节点,如果它为null,那么在某个地方出了问题?你的iframeId在哪里。。。??它是否有父项…?我认为父项设置在这里-form.parentNode.appendChild(iframe)代码>。要查看表单的外观,请检查我编辑的问题,因为我无法将其发布到此处。form.parentNode.removeChild(iframeId)
使用相同的方法删除tooI getMicrosoft JScript运行时错误:DOM异常:未找到错误(8)
,但这似乎是合乎逻辑的。表单是父级。我需要从表单中删除节点。我猜这是表单的子节点。