下面的代碼演示了JS中如何通過(guò)數(shù)組對(duì)象的join方法將數(shù)組元素連接成一個(gè)字符串輸出
<!DOCTYPE html>
<html>
<body>
<p id="demo">
Click the button to join the array elements into a string.
</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana","Orange","Apple","Mango"];
var x=document.getElementById("demo");
x.innerHTML=fruits.join();
}
</script>
</body>
</html>
運(yùn)行結(jié)果如下:
Banana,Orange,Apple,Mango
更多信息請(qǐng)查看IT技術(shù)專欄