使用split()方法
代码示例:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script>
window.onload = function(){
var t = document.getElementById("t");
t.onclick = function(){
var text = "你好,中国,我爱你";
var arr = text.split(","); //将字符串text按照“,”进行分割,然后存入arr数组中
alert(arr[0]+arr[1]+arr[2]);
}
}
</script>
</head>
<body>
<button id="t">点击我</button><br>
</body>
</html>
结果: