当前位置: 代码迷 >> JavaScript >> Uncaught NotFoundError: An attempt was made to reference a Node in a context whe解决思路
  详细解决方案

Uncaught NotFoundError: An attempt was made to reference a Node in a context whe解决思路

热度:1714   发布时间:2013-12-15 22:17:18.0
Uncaught NotFoundError: An attempt was made to reference a Node in a context whe
本人最近在做一个留言版块,遇到了以下问题,百思不得其解。
追求的效果是这样的:点击一个留言的回复按钮,在该留言下方弹出回复界面,同时交替按住不同留言的回复,回复窗口可以交替出现。
留言部分是用ajax显示的。回复按钮的js为:
var flag=1;

function open_response(obj){

var Pnode=obj.parentNode;

if(flag%2)
{
var Cnode=document.createElement('div');

Cnode.id="response";

Cnode.innerHTML="<div align='center'><textarea name='response_comment' rows='5'></textarea></div>"+
  
  "<div class='name'>用户名: <input type='text' name='response_name' style='width:80px' />&nbsp;&nbsp;邮箱: <input type='text' name='response_email'></div>"+
   
  "<div>"+
"<input type='submit' name='response_submit' class='submit' value='回 复' />"+
   "</div>"+
"<div style='clear:both'></div>";

Pnode.appendChild(Cnode);

Cnode.style.display=="block"?Cnode.style.display="none":Cnode.style.display="block";

flag=0;

}
else
{
close_response(Pnode)

}

}

function close_response(obj)
{
var Cnode_1=document.getElementById('response');
obj.removeChild(Cnode_1);
flag=1;
}


结果是交替现象失效,ie浏览器提示的是参数无效,chrome提示:Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist. 

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var flag=1;
 
function open_response(obj){
     
        var Pnode=obj;
         
        if(flag%2)
        {       
        var Cnode=document.createElement('div');
         
        Cnode.id="response";
 
                Cnode.innerHTML="<div align='center'><textarea name='response_comment' rows='5'></textarea></div>"+
                           
                      "<div class='name'>用户名: <input type='text' name='response_name' style='width:80px' />&nbsp;&nbsp;邮箱: <input type='text' name='response_email'></div>"+
                
                          "<div>"+
                                "<input type='submit' name='response_submit' class='submit' value='回 复' />"+
                       "</div>"+
                        "<div style='clear:both'></div>";
                         
                Pnode.appendChild(Cnode);
                 
            Cnode.style.display=="block"?Cnode.style.display="none":Cnode.style.display="block";
  相关解决方案