简体   繁体   English

为什么document.write()之后的代码没有被执行?

[英]Why is code after document.write() not executed?

I have the follwing JavaScript. 我有以下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>

The problem is, the first line of the JS function is executing and the rest are ignored. 问题是,JS函数的第一行正在执行,其余部分被忽略。 If I comment out the first line the rest of the code is executed. 如果我注释掉第一行,则执行其余代码。 Can anybody explain to me why it is so? 任何人都可以向我解释为什么会这样吗?

Because calling document.write implicity calls document.open , which clears the document on which it has been called: 因为调用document.write implicity会调用document.open ,它会清除调用它的文档:

If a document exists in the target, this method clears it 如果目标中存在文档,则此方法将清除它

After the call to document.write , the element you're trying to get a reference to no longer exists in the DOM, and an error is thrown. 在调用document.write ,您尝试获取引用的元素不再存在于DOM中,并且会引发错误。 If you look in the error console it should be something along the lines of the following: 如果您查看错误控制台,它应该是以下内容:

TypeError: Cannot read property 'txt1' of undefined

document.write can only be used during the initial loading of the document. document.write只能在初始加载文档时使用。

If you want to insert your H1 when the function is called, you may replace 如果要在调用函数时插入H1,则可以替换

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

with

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

document.write(content) writes content on the document stream. document.write(content)在文档流上写入内容。

You have to close the stream after writing on the document in order to continue the page loading 您必须在写入文档后关闭流以继续加载页面

document.write("hello");
document.close();

除了破坏答案之外,您还可以将document.write替换为:

document.body.innerHTML += '<h1>Javascript demo</h1>

You're destroying the DOM with your document.write call. 你正在使用document.write调用来破坏DOM。 In some browsers, this also destroys global variables. 在某些浏览器中,这也会破坏全局变量。

Instead use: 而是使用:

var element = document.createElement('h1');
element.appendChild(document.createTextNode('text'));
document.body.appendChild(element);

use document.write() at the end of all JS statements. 在所有JS语句的末尾使用document.write() The script element is never executed after it. script元素永远不会在它之后执行。

Try this: 尝试这个:

var x=document.getElementById(txt1).value;
alert(x);

You can write to the document with 您可以使用以下方式写入文档

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

only once and it applies to the whole document. 只有一次,它适用于整个文件。 If you want to put you will have to add a class/id to it and then put text in that class/id. 如果你想放置,你将不得不为它添加一个类/ id,然后将文本放在该类/ id中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM