JavaScript使用push方法添加一个元素到数组末尾用法实例

前端技术 2023/09/08 JavaScript

本文实例讲述了JavaScript使用push方法添加一个元素到数组末尾的用法。分享给大家供大家参考。具体如下:

下面的代码演示了JS数组通过push方法添加一个元素到数组末尾

<!DOCTYPE html>
<html>
<body>
<p id=\"demo\">
Click the button to add a new element to the array.
</p>
<button onclick=\"myFunction()\">Try it</button>
<script>
var fruits = [\"Banana\",\"Orange\",\"Apple\",\"Mango\"];
function myFunction()
{
fruits.push(\"phpstudy.net\")
var x=document.getElementById(\"demo\");
x.innerHTML=fruits;
}
</script>
</body>
</html>

上面的代码执行后输出如下结果

Banana,Orange,Apple,Mango,phpstudy.net

希望本文所述对大家的javascript程序设计有所帮助。

本文地址:https://www.stayed.cn/item/20568

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。