Why is code after [removed]() not executed?

前端 未结 8 395
长情又很酷
长情又很酷 2021-01-21 07:36

I have the follwing JavaScript.


    
        

        
相关标签:
8条回答
  • 2021-01-21 08:19

    Because calling document.write implicity calls document.open, which clears the document on which it has been called:

    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. If you look in the error console it should be something along the lines of the following:

    TypeError: Cannot read property 'txt1' of undefined

    0 讨论(0)
  • 2021-01-21 08:23

    document.write(content) writes content on the document stream.

    You have to close the stream after writing on the document in order to continue the page loading

    document.write("hello");
    document.close();
    
    • https://developer.mozilla.org/en-US/docs/DOM/document.write
    • https://developer.mozilla.org/en-US/docs/DOM/document.close
    • https://developer.mozilla.org/en-US/docs/DOM/document.open
    0 讨论(0)
提交回复
热议问题