我怎样才能让这个javascript正常工作
How can I get this javascript working?
我把下面的JS直接放在我的WordPress博客网站上。如果你点击一个按钮,它应该随机播放两个音频文件中的一个。
我的问题是:
- JS有什么错误/缺失吗
- 我如何通过按钮启动它
<script>
var playlist = Array();
playlist.push("http://.../wp-content/uploads/2015/03/xxx.mp3");
playlist.push("...wp-content/uploads/2015/03/10-Good-Evening.mp3");
console.log(getSong()); // Here's your song.
document.getElementById("play").addEventListener("click", playSong);
function playSong(){
var audio = new Audio(getSong());
audio.play();
}
function getSong() {
return playlist[Math.floor(Math.random() * playlist.length)];
}
</script>
看起来不错!
只需在javascript标签后添加此html
<button id="play">Click Me</button>
控制台显示:
Navigated to http://fuba-blog.de/randomizer/
VM83:132 Uncaught SyntaxError: Unexpected token <
导航到错误显示
<script>
var playlist = Array();
playlist.push("http://fuba-blog.de/wp-content/uploads/2015/03/alaba-button-mp3.mp3");
playlist.push("http://fuba-blog.de/wp-content/uploads/2015/03/alaba-button-mp3.mp3");</p>
<p>console.log(getSong()); // Here's your song.</p>
<p>document.getElementById("play").addEventListener("click", playSong);</p>
<p>function playSong(){
var audio = new Audio(getSong());
audio.play();
}</p>
<p>function getSong() {
return playlist[Math.floor(Math.random() * playlist.length)];
} </p>
<p></script>
您的脚本中有HTML。
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- ajaxToolkit PopupControlExtender不工作.过时的
- HTML标记在脚本标记中工作
- javascript扫雷器floodfill算法不能正常工作