有人能告诉我为什么这是显示数字而不是字符吗?

Can someone tell me why this is displaying numbers instead of characters?

代码如下:

<!DOCTYPE html>
<html>
<head>
<style>
p {
    visibility: hidden;
}
</style>
</head>
<body>

<pre>Click the button to display the array values after the split.</pre>

<button onclick="myFunction(this)">Try it</button>

<p id="test">"ふ と、目が覚めると 部屋の 中は暗 か"</p>

<script>
function myFunction(p) {
    var str = document.getElementById("test").innerHTML;
    var array = [];
    var array2 = [];
    array.push(str.split(""));
    for (x = 0; x <= str.length; x++) {
        if (array[x] != " ") {
            array2.push(x);
        }
    }
    document.write(array2.toString());
}
</script>

</body>
</html>

我觉得这个问题说得很好,但我必须在post这个问题上添加更多内容。这就是这个底部的原因。对不起。

您正在将循环参数 x 推入第二个数组,而不是第一个数组中的字符。您应该将代码更改为:

array2.push(array[x]);

如果您打算删除第一个字符串中的空格。