jQuery next() 用于类属性

标签 jquery

HTML代码

<span class="Play" href ="media/Tagged - 357.mp3">
<span class="Play" href ="media/Tagged - 358.mp3">
<span class="Play" href ="media/Tagged - 359.mp3">
<span class="Play" href ="media/Tagged - 360.mp3">

我尝试设置歌曲播放器下一首/上一首歌曲按钮。是否可以获得下一个类 attr href?

$("#fwd_btn").on('tap touch click',function () {        
    if(theMP3 !=undefined){
        theMP3.stop();
        currentSong++;
    }

    url_new = $(".Play").next().attr('href');
    playSound(url_new);

});
<小时/>

好吧,看看你的实际代码,跨度看起来不像你上面的那样。它们不是 sibling ,它们位于元素内部,因此 next() 将失败。另外不确定为什么 span 有 href,最好使用 data 属性。

其次我进行了测试

enter image description here

它表明它找到了一个元素。因此,除非 currentSong 没有正确的值,否则我不确定为什么这不起作用。

代码中的内容是 $(this).find(".Play")这意味着您正在寻找 <button><span class="Play" /></button>我知道这不是你所拥有的。

最佳答案

那么你怎么知道什么是活跃的呢?

如果有类(class)

url_new = $(".Play.active").next(".Play").attr('href');

如果没有类,则使用索引

url_new = $(".Play").eq(currentSong).attr('href');
// might need to subtract 1 from currentSong...

关于jQuery next() 用于类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945793/

相关文章:

php - 为什么我看不到使用 jquery post 从 PHP 返回的 JSON 数据?

javascript - 如何使用jquery获取html表中的子项数量

javascript - onClientClick 返回 true 时不会触发 OnClick 事件

jquery - 使用 jQuery 根据字体系列更改正文字体大小

Javascript json 数据在每个附加内容中被 trim

php - 当库加载到页脚时,基于 PHP 有条件调用 Javascript

javascript - jQuery 动画滞后

jquery - 响应地在另一个 Div 中将 Vert&Hori 居中,宽度为 :100% and padding-bottom:100%

php - 在动态 div 中显示下拉菜单

Javascript .load() 函数未执行