Let the contents of the p tag appear in the front

results I expect


1*1=1  
2*1=2  2*2=4  
3*1=3  3*2=6  3*3=9  
4*1=4  4*2=8  4*3=12  4*4=16  
5*1=5  5*2=10  5*3=15  5*4=20  5*5=25  
6*1=6  6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  
7*1=7  7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  
8*1=8  8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  
9*1=9  9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81 

js

for(var i=1;i<=9;iPP) 
{
    for(var j=1;j<=i;jPP)
    {
        document.write(i + "*" + j + "=" + i*j);
        document.write("");
    }
    document.write("<br>");
}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    

<script src="koujue.js"></script> </body> </html>

place the script under the p tag to achieve the desired output.

I want to put the contents of script in head. Excuse me, how can I modify other parts?
html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="koujue.js"></script>  
</head>
<body>
    

</body> </html>

if the other parts are not modified, the result will be:

1*1=1  
2*1=2  2*2=4  
3*1=3  3*2=6  3*3=9  
4*1=4  4*2=8  4*3=12  4*4=16  
5*1=5  5*2=10  5*3=15  5*4=20  5*5=25  
6*1=6  6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  
7*1=7  7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  
8*1=8  8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  
9*1=9  9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  
Mar.13,2021

The

script is to be placed in head , and the content of the script is to be displayed in body , so we need to put the content in the onload function, because when we append the content to body , the body tag must exist

.
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title></title>
  <script>
    function createEl(el) {
      return document.createElement(el);
    }
    window.onload = function() {
      var fra = document.createDocumentFragment();
      for (var i = 1; i <= 9; iPP) {
        var div = createEl('div');
        for (var j = 1; j <= i; jPP) {
          div.textContent += (i + "*" + j + "=" + i * j + " ");
          fra.appendChild(div);
        }
      }
      document.body.appendChild(fra);
    }
  </script>
</head>

<body>
  

</body> </html>

your approach is not appropriate, in document.write is to modify the document stream, if you use this method, you should put script in the need to insert the question, if you use document.write, in head of course inserted in front of the body, the above answer said to use the windows.onload method to implement, is not appropriate, because when windows.onload, the document stream has been closed, if you do document.write, at this time all the content before will be erased.

so, if you want to add content to the page in head, you can't use document.write,. You should use document.body.append,. Of course, this code should also be placed in window.onload to make sure that body is loaded.


use window.onload=function () {/ / js code}


add a div after P


use appendChild bar

using document.write in the onload event triggers an operation to clear the document ( Note: because document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open, which clears the document. ), which clears the previous p tag

Menu