动态添加链接,在添加音频后返回false不起作用

时间:2011-07-30 23:02:40

标签: javascript jquery html5-audio

我已动态添加章节链接,点击后会显示章节的说明(从localStorage获取数据)

$('#chapterList #chapter_ul a').live('click', function(){   
var chID = $(this).attr("id");
$('#chapterDesc p').fadeOut(300).remove();
$('<p></p>').html(localStorage.getItem(chID)).appendTo('#chapterDesc');
$('#chapterDesc p').hide();
$('#chapterDesc p').fadeIn(500);            

return false;

});

上面的代码有效,然后我在回复假线之前点击链接时添加了要播放音频的代码,

if($('#eventAudio').length>0){ //check if there is an existing #eventAudio in DOM
$('#eventAudio')[0].remove(); //removes it
}
else{
    playAudio('audio/','bytes/',soundBytes,0,0); //if no existing, calls the playAudio function
}
if($('#eventAudio').length>0){//checks if there is a new #eventAudio
$('#eventAudio')[0].play();//plays it
}

playAudio功能

    function playAudio(thisLoc,thisSubF,thisArray,thisMusic,looping){
      $('<audio></audio>').attr('id','eventAudio').attr('src',thisLoc+thisSubF+thisArray[thisMusic]).appendTo('#mainContent');
   if(looping==1){
    $('#eventAudio').attr('onended','play()')
   }
}

再次,代码实际上有效,但只有一次。下次我点击链接时,他们只会转到他们链接到的页面。实时(“点击”)功能不再起作用。我已经被困在这个代码上两个小时了。有人,请帮帮忙?提前谢谢!

编辑:我正在Safari 5.1上测试它

1 个答案:

答案 0 :(得分:0)

要从DOM中删除元素

$('#eventAudio').remove(); 

使用$('#eventAudio')[0]时,它返回没有.remove()方法的DOM元素(而不是jQuery对象)。

因此,该行会导致脚本失败并且永远不会执行return false,并且会跟踪链接。