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

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

I have the follwing JavaScript.


    
        

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

    With addition to dystroy answer, you could replace document.write with:

    document.body.innerHTML += '<h1>Javascript demo</h1>
    
    0 讨论(0)
  • 2021-01-21 08:05

    use document.write() at the end of all JS statements. The script element is never executed after it.

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

    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.

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

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

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

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

    with

    var h1 = document.createElement('h1');
    h1.innerHTML = " Just a javascript demo";
    document.body.appendChild(h1);
    
    0 讨论(0)
  • 2021-01-21 08:16

    You're destroying the DOM with your document.write call. In some browsers, this also destroys global variables.

    Instead use:

    var element = document.createElement('h1');
    element.appendChild(document.createTextNode('text'));
    document.body.appendChild(element);
    
    0 讨论(0)
  • 2021-01-21 08:18

    Try this:

    var x=document.getElementById(txt1).value;
    alert(x);
    
    0 讨论(0)
提交回复
热议问题