insertBefore()





js/script.js
  • function funMake() {
  •  var val = document.getElementById('d1');
  •  var elP = document.createElement('P');
  •  elP.innerHTML = 'Hello World!';
  •  val.insertBefore(elP, val.childNodes[1]);
  •  
  •  elP = document.createElement('P');
  •  elP.innerHTML = 'To be or not to be?';
  •  val.insertBefore(elP, null);
  • }
  • function funMake() {
  •  var val = document.getElementById('d1');
  •  var elP = document.createElement('P');
  •  elP.innerHTML = 'Hello World!';
  •  val.insertBefore(elP, val.childNodes[1]);
  •  
  •  elP = document.createElement('P');
  •  elP.innerHTML = 'To be or not to be?';
  •  val.insertBefore(elP, null);
  • }


index.html
  • <!DOCTYPE html>
  • <html>
  • <head>
  •  <meta charset="utf-8" />
  •  <title>New Page</title>
  •  <script src="js/script.js"></script>
  • </head>
  • <body>
  •  <div id="d1">
  •   <p>Привет Мир!</p>
  •  </div>
  •  <script>
  •   funMake();
  •  </script>
  • </body>
  • </html>
  • <!DOCTYPE html>
  • <html>
  • <head>
  •  <meta charset="utf-8" />
  •  <title>New Page</title>
  •  <script src="js/script.js"></script>
  • </head>
  • <body>
  •  <div id="d1">
  •   <p>Привет Мир!</p>
  •  </div>
  •  <script>
  •   funMake();
  •  </script>
  • </body>
  • </html>