1

你能看看这个函数并告诉我错误在哪里吗?Firebug 说“字符串未定义”......感谢任何帮助。(链接在上面声明,console.debug(string) 显示一个逗号分隔的字符串)

function adRotate() {
    var id = Math.floor(Math.random()*links.length);
    var string = links[id];
    var item = string.split(',');
    console.debug(item);
}
4

1 回答 1

1

代码应该可以工作。如果控制台“显示逗号分隔的字符串”,这应该是字符串或数组。

如果adRotate(["one,link", "second,link,"])- 链接是Array- 你会得到:

function adRotate(links) {
    var id = Math.floor(Math.random()*links.length); // valid index in links
    var str = links[id]; // selects one of the links: str is a String
    var item = str.split(','); // splits the string to an Array
    console.debug(item); // logs the array
}

可能的结果:["one","link"]["second","link"]

如果adRotate("one link, and second")- 链接是String- 你会得到:

function adRotate(links) {
    var id = Math.floor(Math.random()*links.length); // valid index in links
    var str = links[id]; // selects one of the chars in the string: str is a String of length 1
    var item = str.split(','); // splits the one-char-string to an Array
    console.debug(item); // logs the array
}

可能的结果:["o"], ["n"], ["e"], [" "], ..., ["k"], ["",""](用于逗号字符), ["i"]等。

于 2012-05-21T18:52:13.147 回答