我正在尝试拆分google这个词并将其存储为数组元素,但它无法正常工作
<!DOCTYPE html>
<html>
<body>
<p>
Click "Try it" to display the first array element, after a string split.
</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "google";
var arr = str.split("");
var txt = arr[0];
document.getElementById("demo").innerHTML = arr[0];
</script>
</body>
</html>
答案 0 :(得分:0)
按预期工作......
function myFunction() {
var str = "google";
var arr = str.split("");
var txt = arr[0]; //Why are you setting this
document.getElementById("demo").innerHTML = arr[0]; //if you're not using that variable anyway
}
&#13;
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
&#13;
虽然我不明白为什么你要分开""
。