【问题标题】:How do I target this specific progress tag?如何定位这个特定的进度标签?
【发布时间】:2014-07-15 03:50:21
【问题描述】:

我试图在我的网页上创建一个音频预览页面,其中每个音频元素都有自己的进度条。但是,我遇到了麻烦,让每个音频元素仅针对其进度条。我对此很陌生,所以我希望这不是一个直截了当的愚蠢问题,但我非常感谢您的帮助,提前致谢。

这是每首歌曲预览的 html。

<audio id="player"></audio>

<div class="songPreview">
  <div class="disp">
    <div class="button" data-src="EXAMPLE.wav" data-pos="0">
      <a href="#" class="soundPlay"><img src="Buttons/play.png"></a>
      <a href="#" class="soundStop hidden"><img src="Buttons/stop.png"></a>
    </div>
    <div class="songInfo">
      <p>SONG NAME HERE</p>
    </div>
  </div>

  <progress class="seekbar" value="0" max="1" style="width:250px"></progress>
</div>

<div class="songPreview2">
  <div class="disp">
    <div class="button" data-src="EXAMPLE2.wav" data-pos="0">
      <a href="#" class="soundPlay"><img src="Buttons/play.png"></a>
      <a href="#" class="soundStop hidden"><img src="Buttons/stop.png"></a>
    </div>
    <div class="songInfo">
      <p>SONG NAME HERE</p>
    </div>
  </div>

  <progress class="seekbar" value="0" max="1" style="width:250px"></progress>
</div>

这就是 JS

$(document).ready(function() {
    var myAudio = document.getElementById("player");
//play stop button etc.
    $(document).on('click', '.soundPlay', function(e) {
        var target = this;
        $(this).hide();
        $(this).next("a").show();
        myAudio.src = target.parentNode.getAttribute('data-src');
        myAudio.play();
        e.preventDefault();
    });


    $(document).on('click', '.soundStop', function(e) {
        var target = this;
        $(this).hide();
        $(this).prev("a").show();
        $('audio').each(function(){
            this.pause();
            this.currentTime = 0;
        });
        e.preventDefault();
    });
    //end of play stop button

    //seekbar
    $('#player').on('timeupdate', function() {
        $('.seekbar').attr("value", this.currentTime / this.duration);
    });
    //end seek bar

    //auto switch button on stop
    var audio = document.getElementById('player');
    audio.addEventListener('ended', stopAudio);

    function stopAudio() {
        audio.stop;
        $('.soundPlay').show('slow');
        $('.soundStop').hide('fast');
        $('.seekbar').attr(this.currentTime = 0);
    }
});

我似乎无法弄清楚如何仅针对每个相应 div 中的进度标签。 无论如何,我可以在没有一百万个不同的进度 ID 和过度重复的 JS 代码的情况下做到这一点吗? (因为一个页面上会有 100 首这样的歌曲预览)

提前致谢!

【问题讨论】:

  • 我只看到一个audio 元素。您如何将src 分配给audio。贴出整个JS代码。
  • 您的标记有些混乱。您有一个音频元素#player,但您有两个 div,每个 div 都有一个进度元素。你能澄清一下吗?
  • 对不起那些家伙,我用整个 JS 代码更新了这个问题。基本上,音频标签是在 div 之外定义的,并从按钮类中的 data-src 获取其数据。但是,一旦播放了任何歌曲,所有进度条都会移动,但我只需要定位单个 songPreview div 中的进度条。感谢 ShaunakD 和 @user3558931

标签: javascript jquery audio progress-bar progress


【解决方案1】:

您必须使用data-src 跟踪当前正在播放的声音div

使用.val() 而不是attr('value')

试试这个,

$('#player').on('timeupdate', function() {
    $('div[data-src="'+this.src+'"]').parents('.disp').siblings('.seekbar').val(this.currentTime / this.duration);
});

还有,

stopAudio()改成这个

function stopAudio() {
    audio.pause();
    audio.currentTime = 0;
    $('.soundPlay').show('slow');
    $('.soundStop').hide('fast');
    $('.seekbar').val(0);
}

DEMO

【讨论】:

  • 嗯,不走运 :( 现在酒吧根本不会移动......不知道我做错了什么。还是谢谢!
  • 我不知道,你的在 jsfiddle 中工作,但在 Dreamweaver 中没有。不,没有错误。此外,在您的演示中,两个进度条都会移动,而不仅仅是一个。附言。非常感谢您花时间提供帮助,非常感谢。
  • 两者都有效,因为我为两者指定了相同的链接。检查这个。 jsfiddle.net/ReeXf/3。你有任何错误吗?您的 Dreamweaver 是否支持 progress 标签?只支持 HTML5 你用过&lt;!DOCTYPE html&gt; 吗?
  • 好的,我现在完全复制了您的代码,它可以工作:D. 我意识到问题是,由于某种原因,当我将 data-src 更改为
    但它适用于您提供的示例链接。
  • 请忽略最后一条评论。它适用于小提琴中的链接类型,即使文件格式相同,只是由于某种原因,链接本地文件时条不会移动。你知道为什么吗?谢谢
猜你喜欢
相关资源
最近更新 更多
热门标签