1

抱歉,如果标题不清楚。我有一个 swiperight 和 swipeleft 事件,当触发时将音频源添加到具有自动播放功能的 html5 音频标签中。因此,如果您向左滑动播放一首歌曲并向右滑动另一首播放..问题是它只能播放一次。如果我向左滑动歌曲播放然后如果我向右滑动什么,再次向左滑动什么,反之亦然。有没有办法重置功能?希望这是有道理的,谢谢!下面是我的代码

<script>
$(document).ready(function() {
    $("body").bind("swiperight", function(event){
        $('#swipe').append(
'<source src="http://a.tumblr.com/tumblr_ljdvcbDIsN1qhk4f9o1.mp3"/>');
    })

    $("body").bind("swipeleft", function(event) {
        $('#swipe').append('<source src="http://www.nerdthegame.com/media/audio/soundtrack/Nintendo-Super-Mario-Theme-Song.mp3" />');
    })
})

4

1 回答 1

1

我相信 swipe 事件处理程序每​​次都会执行,但是随后您将.append()添加<source>元素到您的<audio>元素中,这样它最终会包含多个<source>元素。尝试先删除前<source>一个,您可以.empty()按如下方式进行操作:

$(document).ready(function() {
    $("body").bind("swiperight", function(event){
        $('#swipe').empty().append(
'<source src="http://a.tumblr.com/tumblr_ljdvcbDIsN1qhk4f9o1.mp3"/>');
    })

    $("body").bind("swipeleft", function(event) {
        $('#swipe').empty().append('<source src="http://www.nerdthegame.com/media/audio/soundtrack/Nintendo-Super-Mario-Theme-Song.mp3" />');
    })
})

似乎然后在小提琴中工作(我没有在演示中使用滑动事件,因为我在笔记本电脑而不是手机上进行测试):http: //jsfiddle.net/AG3Tu/

于 2013-07-03T04:12:05.723 回答