源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <p>unshift方法返回新数组的长度.</p> <button onclick="myFunction()">试试</button> <p id="demo"></p> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits; function myFunction() { document.getElementById("demo").innerHTML = fruits.unshift("Lemon"); } </script> <p><b>注意:</b> unshift() 方法在Internet Explorer 8和早期版本不能正常工作,可以插入值,但返回值是 <em>undefined</em>.</p> </body> </html>
运行结果