动态添加的链接在添加音频后返回 false 不起作用
我动态添加了章节的链接,单击该链接时,会显示章节的描述(从 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;
});
上面的代码有效,然后我添加了在 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()')
}
}
,代码实际上有效,但仅一次。下次我单击链接时,它们只会转到链接到的页面。 live('click') 功能不再起作用。我已经被这个代码困住了两个小时了。有人请帮忙吗?提前致谢!
编辑:我正在 Safari 5.1 上测试此功能
I have dynamically added links for Chapters that when clicked, displays the description of the chapter (gets the data from 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;
});
the code above works, then I added code for an audio to play when the link is clicked just before the return false line,
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
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()')
}
}
again, the code actually works, but just once. The next time I click on the links, they just go to the pages that they are linked to. the live('click') function doesn't work anymore. I've been stuck on this code for two hours now. Anybody, please help? Thanks in advance!
EDIT: I am testing this on Safari 5.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要从 DOM 中删除元素,请使用
当您使用
$('#eventAudio')[0]
时,它会返回 DOM 元素(而不是 jQuery 对象),该元素不会有一个 .remove() 方法。因此,该行会导致脚本失败,并且
return false
永远不会执行,并且会遵循链接。To remove the element from the DOM use
When you use
$('#eventAudio')[0]
it returns the DOM element (and not the jQuery object) which does not have a.remove()
method.So, that line causes the script to fail and the
return false
is never executed, and the link is followed..