按下按钮时插入声音

时间:2014-05-11 10:13:46

标签: javascript jquery html cordova

按下开始按钮时我必须发出声音。我在互联网上建立了以下代码,但不起作用:

  playSound function (soundfile) {
                            document.getElementById ("dummy"). innerHTML = 
                            "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"; 
                        } 

<input type="button" class="finish" onclick="playSound('button.wav')" id="pulsante1"/> 

我不明白为什么,也许是因为按下时我的按钮已经是动作了?即:

 $ (this). on ('click', refreshValues​​); 

和此:

$ ("input.finish.") on ("click", function () {
                                    tot + = parseInt ($ (this). val ()); 
                                    document.getElementById ("total"). value = tot; 
                                    }); 

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我使用这样的东西:

function playSound() 
{
    $('audio').remove(); 
    $('body').append('<audio class="sound"><source src="/assets/sounds/alert.mp3"></source><source src="./sounds/alert.ogg"></source></audio>');
    $('.sound')[0].play();
}

$('#startbtn').on('click', playSound​​); 
相关问题