下面的代码演示了JS中如果通过数组的reverse方法将数组元素反转过来
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to reverse the order of the elements in the array.</p> <button onclick="myFunction()">Try it</button> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; function myFunction() { fruits.reverse(); var x=document.getElementById("demo"); x.innerHTML=fruits; } </script> </body> </html>
上面的代码输出如下结果,所有的数组元素都颠倒过来了
Mango,Apple,Orange,Banana