简体   繁体   English

使用表时,jQuery Next()无法正确突出显示下一个元素

[英]Jquery Next() not highlighting next element correctly when using a table

I have a table with each row representing a song. 我有一张桌子,每一行代表一首歌。

When a song is clicked, the parent td should be highlighted a light blue color with the .active class and if any song was highlighted previously the parent td's .active class should be removed. 单击歌曲时,应使用.active类将父td突出显示为浅蓝色,如果以前突出显示了任何歌曲,则应删除父td的.active类。

This part works fine and is represented with this jquery: 这部分工作正常,并用此jquery表示:

$(".songs").click(function(){
    $('.songs').parents('td').removeClass('active');
    $(this).parents('td').addClass('active');
});

I also want to have a next button and a previous button. 我也想有一个下一个按钮和一个上一个按钮。 This where I am having issues. 这是我遇到的问题。 When the next button is clicked, the next song on the list should be highlighted and the previously highlighted song should be unhighlighted (I am using the class .active to do the highlighting and unhighlighting). 单击下一个按钮时,列表中的下一首歌曲应突出显示,而先前突出显示的歌曲应不突出显示(我正在使用.active类进行突出显示和不突出显示)。 This part is not working: 这部分不起作用:

$('#next_button').click(function(){
    var current = $('td.active');
    $('.songs').parents('td').removeClass('active');
    current.nextAll('td:first').addClass('active');
});

Here is the jsfiddle link: 这是jsfiddle链接:

jsfiddle Link jsfiddle链接

Here is my html code: 这是我的html代码:

<table id="song_table">
    <thead id="song_thead">
    <tr>
        <th id="table_head">Songs</th>
    </tr>
    </thead>
    <tbody id="song_tbody">
        <tr>
            <td class="td_songs">
                <a class="songs">
                    1
                </a>
            </td>
        </tr>
        <tr>
            <td class="td_songs">
                <a class="songs">
                    2
                </a>
            </td>
        </tr>
    </tbody>
</table>
<div id="next_button">
    <p id="next_text">Next Button</p>
</div>

Here is my css: 这是我的CSS:

.active{
    background-color: #D9FAFA;
}
table{
    text-align: center;
    height: 100px;
    width: 200px;
}
#table_head{
    text-align: center;
}
#next_button{
    height: 100px;
    width: 200px;
    background-color: lightgreen;
}

Here is my jquery 这是我的jQuery

$(document).ready(function() {
    $(".songs").click(function(){
        $('.songs').parents('td').removeClass('active');
        $(this).parents('td').addClass('active');
    });
    $('#next_button').click(function(){
        var current = $('td.active');
        $('.songs').parents('td').removeClass('active');
        current.nextAll('td:first').addClass('active');
    });
});

If you could help me solve this issue, I would greatly appreciate it. 如果您能帮助我解决这个问题,我将不胜感激。 I feel like this should be so easy but I just can't seem to make it work. 我觉得这应该很容易,但是我似乎无法使其正常工作。

Thanks! 谢谢!

The trick is to get the row index of the current song, add 1, and then do a modulo with number of rows that way if the current row+1 overflows the number of rows, it will start from the beginning: 诀窍是获取当前歌曲的行索引,将其加1,然后对行数进行模运算,如果当前行+1超出了行数,它将从头开始:

 $().ready(function() { $(".songs").click(function(){ $('.songs').parents('td').removeClass('active'); $(this).parents('td').addClass('active'); }); $('#next_button').click(function(){ //here .parent() will get the current <tr> //.parent().index() will get the index of the current <tr> var currentID = $('td.active').parent().index(); //here .parent() will get the <tr> //.parent().parent() will get the <tbody> //.parent().parent().children() will get all the rows //.parent().parent().children().length will get the row count var nextID=(currentID+1)%($('td.active').parent().parent().children().length) $('.songs').parents('td').removeClass('active'); $('td').eq(nextID).addClass('active'); }); }); 
 .active{ background-color: #D9FAFA; } table{ text-align: center; height: 100px; width: 200px; } #table_head{ text-align: center; } #next_button{ height: 100px; width: 2d00px; background-color: lightgreen; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="song_table"> <thead id="song_thead"> <tr> <th id="table_head">Songs</th> </tr> </thead> <tbody id="song_tbody"> <tr> <td class="td_songs"> <a class="songs"> 1 </a> </td> </tr> <tr> <td class="td_songs"> <a class="songs"> 2 </a> </td> </tr> <tr> <td class="td_songs"> <a class="songs"> 3 </a> </td> </tr> <tr> <td class="td_songs"> <a class="songs"> 4 </a> </td> </tr> </tbody> </table> <div id="next_button"> <p id="next_text">Next Button</p> </div> 

in your code you have each td in its own tr meaning there is no next td to go to. 在您的代码中,每个td都有自己的tr这意味着没有下一个td可以去。

you should adjust your jquery to focus on the rows, as in this fiddle (shown below) 您应该调整jquery使其专注于行,如下面的小提琴所示(如下所示)

$().ready(function() {
    $(".songs").click(function(){
        $('.songs').parents('tr').removeClass('active');
        $(this).parents('tr').addClass('active');
    });
    $('#next_button').click(function(){
        var current = $('tr.active');
        $('.songs').parents('tr').removeClass('active');
        current.next('tr').addClass('active');
    });
});

You'll also notice I'm using .next() which will just grab the next element or the next element which matches the argument (in this case tr ) - no need to get all then restrict to just the first. 您还会注意到我正在使用.next() ,它将仅捕获下一个元素或与参数匹配的下一个元素(在本例中为tr )-无需全部获取,然后仅限制为第一个。

All this will make your fiddle behave as expected, however, if you want to target the td 's within each of the tr 's you'll have to add .find('td') to get the td out of the retrieved tr , like this . 所有这些将使您的提琴琴表现出预期的效果,但是,如果要在每个tr定位td ,则必须添加.find('td')才能从检索到的tr获取td这样 Here the only line that is changed is the one that adds the class on click of next, which is now: current.parent().next('tr').find('td').addClass('active'); 这里唯一更改的行是在单击“ next”时添加类的行,即: current.parent().next('tr').find('td').addClass('active');

Refactoring out $('.songs').parents('tr').removeClass('active'); 重构$('.songs').parents('tr').removeClass('active'); into it's own function would also clear your code a bit and make it easier to follow, a good habit! 进入它自己的功能还可以清除您的代码并使其易于遵循,这是一个好习惯! (also +1 for using a variable to store a returned JQuery DOM object - var current = $('tr.active'); - another good habit for code clarity and efficiency, especially when you are deraling with more complicated DOM structures and functions) (对于使用变量存储返回的JQuery DOM对象也为+1- var current = $('tr.active'); -另一个提高代码清晰度和效率的好习惯,尤其是当您使用更复杂的DOM结构和功能时)

Something like this? 像这样吗 http://jsfiddle.net/y5ntap04/3/ http://jsfiddle.net/y5ntap04/3/

You needed to go up the DOM and then where all the siblings are, you can go to the next() one. 您需要上移DOM,然后所有兄弟姐妹都在哪里,可以转到next()一个。

Plus added a previous button for you. 加号为您添加了上一个按钮。

$().ready(function () {
    $(".songs").click(function () {
        $('.songs').parents('td').removeClass('active');
        $(this).parents('td').addClass('active');
    });
    $('#next_button').click(function () {
        $('.songs').parents('td.active').removeClass('active').closest('tr').next().find('td').addClass('active');
    });
    $('#previous_button').click(function () {
        $('.songs').parents('td.active').removeClass('active').closest('tr').prev().find('td').addClass('active');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM