Javascript 如何显示从列表生成的随机项?
生成“randomSong”后,我希望它显示如下句子: 用这个!代码(我随机生成的代码)命令在我们的聊天论坛兑换溢价Javascript 如何显示从列表生成的随机项?,javascript,html,Javascript,Html,生成“randomSong”后,我希望它显示如下句子: 用这个!代码(我随机生成的代码)命令在我们的聊天论坛兑换溢价 函数findSong(){ var song=[“xikskx”、“iwnujk”、“ldilxb”、“hgbhrw”、“ijkczb”]; var random=Math.floor(Math.random()*(song.length-1)); document.getElementById(“随机歌曲”).setAttribute(“值”,歌曲[random]); }
函数findSong(){
var song=[“xikskx”、“iwnujk”、“ldilxb”、“hgbhrw”、“ijkczb”];
var random=Math.floor(Math.random()*(song.length-1));
document.getElementById(“随机歌曲”).setAttribute(“值”,歌曲[random]);
}
生成高级代码
您可以将
元素添加到html代码中,用于显示新生成的文本
<div>
<button onclick="findSong();" type="randomSong">Generate Premium Code</button>
<input "RandomCode" id="randomSong">
<!-- new code -->
<p id="text"></p>
</div>
fiddle:请在问题中发布理解问题所需的代码。另请看一看:请进一步澄清您的问题
function findSong() {
var song = ["xikskx", "iwnujk", "ldilxb", "hgbhrw", "ijkczb"];
var random = Math.floor(Math.random() * (song.length - 1));
document.getElementById("randomSong").setAttribute("value", song[random]);
// new code
var premium = document.getElementById('text');
premium.textContent = 'Use the code ' + song[random] + ' command in our chat forums to redeem premium.';
}