Javascript无法拆分字符串

时间:2015-01-22 02:33:53

标签: javascript arrays split

我正在尝试拆分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>

1 个答案:

答案 0 :(得分:0)

按预期工作......

&#13;
&#13;
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;
&#13;
&#13;

虽然我不明白为什么你要分开""