开发者

Select a random string from an array [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

JavaScript: Getting random value from an array

Could someone help me on this topic? I've got this code.

var textArray = [
    'song1.ogg',
    'song2.ogg'
]
audioElement.setAttr开发者_如何学JAVAibute('src', textArray);

How can I randomly get one of those strings into my audio element?

Would be glad if someone can help....


var textArray = [
    'song1.ogg',
    'song2.ogg'
];
var randomNumber = Math.floor(Math.random()*textArray.length);

audioElement.setAttribute('src', textArray[randomNumber]);


var randomIndex = Math.floor(Math.random() * textArray.length); 
var randomElement = textArray[randomIndex];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜