欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

舉例講解JavaScript中將數(shù)組元素轉(zhuǎn)換為字符串的方法

 更新時(shí)間:2015年10月25日 15:00:22   投稿:goldensun  
這篇文章主要介紹了JavaScript中將數(shù)組元素轉(zhuǎn)換為字符串的方法,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

首先來看一下從一個(gè)數(shù)組中選擇元素的方法slice():
源代碼:

<!DOCTYPE html>
<html>
<body>
​
<p id="demo">Click the button to extract the second and the third elements from the array.</p>
​
<button onclick="myFunction()">Try it</button>
​
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);
var x=document.getElementById("demo");
x.innerHTML=citrus;
}
</script>
​
</body>
</html>   

測(cè)試結(jié)果:

Orange,Lemon

我們可以用數(shù)組的元素組成字符串,相關(guān)的join()方法使用例子:

源代碼:

<!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> 

測(cè)試結(jié)果:

Banana,Orange,Apple,Mango

直接轉(zhuǎn)換數(shù)組到字符串則可以用toString()方法:
源代碼:

<!DOCTYPE html>
<html>
<body>
​
<p id="demo">點(diǎn)擊按鈕將數(shù)組轉(zhuǎn)為字符串并返回。</p>
​
<button onclick="myFunction()">點(diǎn)我</button>
​
<script>
function myFunction()
{
 var fruits = ["Banana", "Orange", "Apple", "Mango"];
 var str = fruits.toString();
 var x=document.getElementById("demo");
 x.innerHTML= str;
}
</script>
​
</body>
</html>

測(cè)試結(jié)果:

Banana,Orange,Apple,Mango 

相關(guān)文章

最新評(píng)論