我试图让浏览器显示警报,如果音频元素src属性指向不存在的文件,但是如果我附加"错误"事件,我不会得到任何响应.
这是我的问题和我尝试过的问题http://jsfiddle.net/Xx2PW/7/
HTML:
<p>Non existent audio files - should return an error
<audio preload="auto">
<source src="http://example.com/non-existant.wav" />
<source src="http://example.com/non-existant.mp3" />
<source src="http://example.com/non-existant.ogg" />
</audio>
</p>
<p>Existing audio file - should just play
<audio preload="auto">
<source src="http://jplayer.org/audio/m4a/Miaow-07-Bubble.m4a" />
</audio>
</p>
Run Code Online (Sandbox Code Playgroud)
和JS:
playerMarkup = "<a class=\"player\">Play</a>";
audioTags = $("audio");
audioTags.before(playerMarkup); //insert markup before each audio tag
$(".player").on("click", function () {
currentAudio = $(this).next()[0];
//I've tried catching the error like this - no effect
alert("Trying to play file.");
try {
currentAudio.play();
} …Run Code Online (Sandbox Code Playgroud)