appendChild在函数外部工作,但不在内部工作

时间:2016-05-14 18:12:56

标签: javascript html appendchild

我的代码的某些部分出了问题。我尝试使用JavaScript创建“DIV”和“P”标签,但只有当我将代码放在函数外部时才会起作用(函数称为“fo”) 。当您单击该按钮时,会出现一个对话框,如果您单击取消,则appendChild方法应将“div”和“p”标记放在“body”中。

我应该补充说,p标签中的文字可以在屏幕上短暂显示,然后突然消失。我的浏览器是Google Chrome。

<html>
<body>
</body>
<script> 
function give(){
form = document.createElement("FORM");
input = document.createElement("INPUT");
input.setAttribute("type", "submit");
input.setAttribute("value", "button");
form.setAttribute("id", "abc");
form.setAttribute("onsubmit", "fo()");
textarea = document.createElement("TEXTAREA");
textarea.setAttribute("id", "txt");
form.appendChild(input);
form.appendChild(textarea);
document.body.appendChild(form);
document.getElementById("abc").method = "post";
}
  give();
  function fo(){
a = document.getElementById("txt").value; 
cfm = confirm("Are you sure you want changes");
if (cfm == false ) {
div = document.createElement("DIV");
p = document.createElement("P");
ptxt = document.createTextNode("test");
p.setAttribute("id", "centr");
p.appendChild(ptxt);
div.appendChild(p);
document.body.appendChild(div);
}
}
/*When this part of code is outside function fo() , appendChild works correctly
  div = document.createElement("DIV");
  p = document.createElement("P");
  ptxt = document.createTextNode("Outside the function it works");
  p.setAttribute("id", "centr");
  p.appendChild(ptxt);
  div.appendChild(p);
  document.body.appendChild(div); 
  */
  </script>
  </html>

1 个答案:

答案 0 :(得分:0)

您遇到了表单提交的默认行为,该行为导航到页面,在action属性中指定,或者在未指定action的情况下重新加载当前页面。

您需要对代码进行以下更改才能解决此问题:

  1. 修改提交处理程序注册到form.setAttribute("onsubmit", "fo(event)");
  2. fo()功能签名更改为fo(event)
  3. event.preventDefault()条件正文
  4. 结束时致电if (cfm == false)

    所以你的代码看起来像这样:

    <html>
    <body>
    </body>
    <script>
        function give(){
            form = document.createElement("FORM");
            input = document.createElement("INPUT");
            input.setAttribute("type", "submit");
            input.setAttribute("value", "button");
            form.setAttribute("id", "abc");
            form.setAttribute("onsubmit", "fo(event)");
            textarea = document.createElement("TEXTAREA");
            textarea.setAttribute("id", "txt");
            form.appendChild(input);
            form.appendChild(textarea);
            document.body.appendChild(form);
            document.getElementById("abc").method = "post";
        }
        give();
        function fo(event){
            a = document.getElementById("txt").value;
            cfm = confirm("Are you sure you want changes");
            if (cfm == false ) {
                div = document.createElement("DIV");
                p = document.createElement("P");
                ptxt = document.createTextNode("test");
                p.setAttribute("id", "centr");
                p.appendChild(ptxt);
                div.appendChild(p);
                document.body.appendChild(div);
                event.preventDefault();
            }
        }
    </script>
    </html>
    
相关问题