javascript - jquery/javascript 播放/停止异步问题

标签 javascript jquery audio

我有一个基本上是动态创建的页面(这是关键),在这个页面中我有 div,每个 div 应该播放/暂停一首歌曲。

如果我发出警报,代码就会工作,所以这让我相信存在异步问题。所以我尝试在调用播放/暂停时添加超时,但这也不起作用。

代码现在的行为方式是第一个 div 完美运行,但任何其他 div 都无法工作,除非我发出警报。

有人遇到过这样的问题吗?

这是我的代码,我使用 livequery 来获取动态元素。

<小时/>

编辑:

现在的问题是只播放第一首歌曲,当我单击其他歌曲的播放/暂停按钮时没有任何反应。

function callAction(url) {
    if (url == "music.html") {
        var believe = new Audio('/assets/music/believe.mp3');
        var hope = new Audio('/assets/music/hope.mp3');
        var alf = new Audio('/assets/music/alf.mp3');
        var thatnight = new Audio('/assets/music/thatnight.mp3');
        var lostlove = new Audio('/assets/music/lostlove.mp3');
        var time = new Audio('/assets/music/time.mp3');

        $("body").on("click", ".pausebutton", function(){
            var song = $(this).attr('name');
            if (song != null && song == "believe") {
                believe.pause();
            }
            if (song != null && song == "hope") {
                hope.pause();
            }
            if (song != null && song == "alf") {
                alf.pause();
            }
            if (song != null && song == "thatnight") {
                thatnight.pause();
            }
            if (song != null && song == "lostlove") {
                lostlove.pause();
            }
            if (song != null && song == "time") {
                time.pause();
            }
        });
        $("body").on("click", ".playbutton" ,function(){
            var song = $(this).attr('name');
            if (song != null && song == "believe") {
                believe.play();
            }
            if (song != null && song == "hope") {
                hope.play();

            }
            if (song != null && song == "alf") {
                alf.play();
            }
            if (song != null && song == "thatnight") {
                thatnight.play();
            }
            if (song != null && song == "lostlove") {
                lostlove.play();
            }
            if (song != null && song == "time") {
                time.play();
            }
        });
    }
}

并不是说 html 会真正产生影响,但这是 html:

<div id="music">
    <h1>Music</h1>
    <br><br>
    <div class="row">
        <div class="musicEntry red">
            <center><p class="musicTitle">Believe</p></center>
            <center><p class="musicDate">July 27th 2014</p></center>

            <div class="audio-controls">
                <center><i name="believe" class="fa fa-play-circle fa-3x playbutton"></i></center>
                <center><i name="believe" class="fa fa-pause fa-3x pausebutton"></i></center>
            </div>
        </div>

        <div class="musicEntry red">
            <center><p class="musicTitle">A Little Hope</p></center>
            <center><p class="musicDate">March 8th 2014</p></center>



            <div class="audio-controls">
                <center><i name="hope" class="fa fa-play-circle fa-3x playbutton"></i></center>
                <center><i name="hope" class="fa fa-pause fa-3x pausebutton"></i></center>
            </div>
        </div>


        <div class="musicEntry red">
            <center><p class="musicTitle">Alf Layla</p></center>
            <center><p class="musicDate">April 23rd 2014</p></center>


            <div class="audio-controls">
                <center><i name="alf" class="fa fa-play-circle fa-3x playbutton"></i></center>
                <center><i name="alf" class="fa fa-pause fa-3x pausebutton"></i></center>
            </div>
        </div>
    </div>

     <div class="row">
        <div class="musicEntry red">
            <center><p class="musicTitle">That Night</p></center>
            <center><p class="musicDate">March 21th 2013</p></center>

            <div class="audio-controls">
                <center><i name="thatnight" class="fa fa-play-circle fa-3x playbutton"></i></center>
                <center><i name="thatnight" class="fa fa-pause fa-3x pausebutton"></i></center>
            </div>
        </div>

        <div class="musicEntry red">
            <center><p class="musicTitle">Lost Love</p></center>
            <center><p class="musicDate">July 30th 2012</p></center>



            <div class="audio-controls">
                <center><i name="lostlove" class="fa fa-play-circle fa-3x playbutton"></i></center>
                <center><i name="lostlove" class="fa fa-pause fa-3x pausebutton"></i></center>
            </div>
        </div>


        <div class="musicEntry red">
            <center><p class="musicTitle">Time (Cover)</p></center>
            <center><p class="musicDate">July 26th 2012</p></center>


            <div class="audio-controls">
                <center><i name="time" class="fa fa-play-circle fa-3x playbutton"></i></center>
                <center><i name="time" class="fa fa-pause fa-3x pausebutton"></i></center>
            </div>
        </div>
    </div>
</div>

这只是播放和暂停第一首歌曲。你知道为什么吗?

最佳答案

问题是您的点击事件和动态加载的页面。当 DOM 上有动态添加的元素时,请使用 $(selector).on('click', function(e){}) 而不是点击事件。以下是修改后的处理程序:

$(".playbutton").on("click", function(){
    // Your code goes here
});

关于javascript - jquery/javascript 播放/停止异步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25002333/

相关文章:

asp.net - 调用 window.open 打开一个窗口,但随后它突然最小化

javascript - Firebase toDate() 使用 CEST 而不是 localTime

javascript - 浏览器等待 ajax 调用完成,即使在调用 abort 之后(jQuery)

android - 单击按钮时播放声音的最有效方法

Android - 如何确定是否连接了外部扬声器

javascript - JQuery 无法在 "forwards"css 动画后对 div 进行动画处理

javascript - 为什么函数的交集类型接受其中一个类型声明而不是两者都接受?

javascript - 就像系统 : Looping forms unique id, 使用 jQuery/Javascript 获取这些 id 来单独发布一样

javascript - 单选按钮值未存储在数据库中

python - Python 中的 Discord 机器人。同时播放两个音频文件