为什么未执行document.write()之后的代码? [英] Why is code after document.write() not executed?

查看:47
本文介绍了为什么未执行document.write()之后的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JavaScript.

I have the follwing JavaScript.

<html>
    <head>
        <script language="JavaScript">
            function fdivisible()
            {
                document.write("<h1> Just a javascript demo</h1>");
                var x=document.forms["aaa"]["txt1"].value;  
                alert(x);   
            }
        </script>
    </head>
    <body>
        <form action="#" name="aaa">
            Enter a no. : <input type="text" name="txt1" id="txt1" />
            <input type="button" value="Click" onclick="fdivisible();">
        </form>
    </body>
</html>

问题是,JS函数的第一行正在执行,其余部分被忽略.如果我注释掉第一行,则其余代码将被执行.有人可以向我解释为什么会这样吗?

The problem is, the first line of the JS function is executing and the rest are ignored. If I comment out the first line the rest of the code is executed. Can anybody explain to me why it is so?

推荐答案

document.write can only be used during the initial loading of the document.

如果要在调用函数时插入H1,可以替换

If you want to insert your H1 when the function is called, you may replace

document.write("<h1> Just a javascript demo</h1>");

使用

var h1 = document.createElement('h1');
h1.innerHTML = " Just a javascript demo";
document.body.appendChild(h1);

这篇关于为什么未执行document.write()之后的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆