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

dynamically added links that returns false not working after adding audio

本文关键字:添加 音频 不起作用 false 返回 动态 链接      更新时间:2023-09-26

我为章节动态添加了链接,当点击时,显示章节的描述(从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;
});

上面的代码可以工作,然后我添加了在返回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()')
   }
}
同样,

代码实际上是有效的,但只有一次。下次我点击链接的时候,它们就会转到它们被链接到的页面。live('click')功能不再工作了。我已经被这个密码困了两个小时了。有人来帮忙吗?提前感谢!

EDIT:我正在Safari 5.1上测试这个

使用

从DOM中删除元素
$('#eventAudio').remove(); 

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

所以,这一行会导致脚本失败,并且return false永远不会执行,并且链接紧随其后。