Array.forEach()

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript Array.forEach()</h1>
  6.  
  7. <p>为每个元素调用一次函数。</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var txt = "";
  13. var numbers = [45, 4, 9, 16, 25];
  14. numbers.forEach(myFunction);
  15. document.getElementById("demo").innerHTML = txt;
  16.  
  17. function myFunction(value, index, array) {
  18. txt = txt + value + "<br>";
  19. }
  20. </script>
  21.  
  22. </body>
  23. </html>