Javascript音频播放/停止
问题描述:
仍然有问题 - 如果我的代码播放选定的音频,按钮将不会切换。如果我包含$(document).ready(function(){
,那么按钮切换但音频不会播放!任何人都可以拯救我留下的小发型吗?这是交付在iOS的HTML5 jQuery Mobile的项目5Javascript音频播放/停止
感谢任何/所有帮助 Ĵ
代码:
$(document).ready(function(){
var selSound = '18000'; //default to first sound.
var play = false; //default to paused.
function selectSound(id) {
selSound = id;
}
$('#playbtn').click(function() {
var $this = $(this);
var a = document.getElementById(selSound);
if($this.text() == 'Zap!') {
$this.children().children().text('Stop');
a.play();
} else {
$this.children().children().text('Zap!');
a.pause();
alert('Stopped playing song: '+selectedMP3);
}
});
})
HTML:
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" >
<li data-swatch="b" class="ui-li ui-li-divider ui-btn ui-bar-a ui-corner-top" data-role="list-divider" role="" data-form="ui-bar-b">Select a frequency</li>
<input type="radio" name="mp3_option" id="selectSound('16000')" onclick="selectSound('16000')"><label for="selectSound('16000')">16Hz</label>
<input type="radio" name="mp3_option" id="selectSound('18000')" onclick="selectSound('18000')"><label for="selectSound('18000')">18Hz</label>
<input type="radio" name="mp3_option" id="selectSound('20000')" onclick="selectSound('20000')"><label for="selectSound('20000')">20Hz</label>
</fieldset>
<div style="text-align:center"><a href="#" data-role="button" data-inline="true" id="playbtn">Zap!</a></div></div>
答
好吧,有点这是一个假设,但我认为这是一个范围问题。需要声明的文件准备处理以外的变量和函数,但附加在它里面的按钮事件...
var selSound = '18000'; //default to first sound.
var play = false; //default to paused.
function selectSound(id) {
selSound = id;
}
$(document).ready(function(){
$('#playbtn').click(function() {
var $this = $(this);
var a = document.getElementById(selSound);
if ($this.text() == 'Zap!') {
$this.children().children().text('Stop');
a.play();
} else {
$this.children().children().text('Zap!');
a.pause();
alert('Stopped playing song: ' + selectedMP3);
}
});
})
你应该将'功能selectSound(ID)''的$(文件)之外。就绪(function(){});' – PeeHaa 2012-03-06 16:52:39
我会开始通过改变id属性的值来解决你的问题。通过你的javascript的外观,你正在选择一个无效的id(selSound ='18000')。另见http://*.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – 2012-03-06 17:04:17