在jQuery中按类导航Next或Previous div

kxeu7u2r  于 2023-06-22  发布在  jQuery
关注(0)|答案(1)|浏览(135)

我正在尝试创建导航(下一个和上一个)按钮,它显示类为marked的下一个(或上一个)div。

JSFiddleLink
HTML部分

<table>
<tr id="row_question_container">
<td valign="top" colspan="2">
<div id="at_test_area-1" class="at_test_area">
    <div id="at_questions_container">
        <div id="1" class="question_block unmarked" > 
            Hello world 1
        </div>
        <div id="2" class="question_block marked" style="display: none;">
            Hello world 2
        </div>
        <div id="3" class="question_block unmarked" style="display: none">
            Hello world 3
        </div>
        <div id="4" class="question_block marked" style="display: none">
            Hello world 4
        </div>
        <div id="5" class="question_block unmarked" style="display: none">
            Hello world 5
        </div>
        <div id="6" class="question_block marked" style="display: none">
            Hello world 6
        </div>
        <div id="7" class="question_block marked" style="display: none">
            Hello world 7
        </div>
        <div id="7" class="question_block unmarked" style="display: none">
            Hello world 8
        </div>
    </div>
</div>
</td>
</tr>
</table>
<input type="button" id="previous" value="Previous">
<input type="button" id="next" value="Next">

jQuery Part

$(document).ready(
function () {
    var current_question_number = 0;

    $('#next').click(function () {
        ShowMarkedQuestion("next");
    });

    $('#previous').click(function () {
        ShowMarkedQuestion("previous");
    });

    function ShowMarkedQuestion(mode) {
        var id = $(".question_block").filter(function () {
            if ($(this).css('display') == 'block') {
                return true;
            }
        }).attr('id');
        $('#' + id).hide();
        if (mode === "next") {
            current_question_number = parseInt(id) + 1;
        } else if (mode === "previous") {
            current_question_number = parseInt(id) - 1;
        } else {
            current_question_number = parseInt(id);
        }
        $('#' + current_question_number).show();
    }
});

我了解到,为了得到下一个div类marked,我需要使用find()children()像-

var marked_question = $('#at_questions_container').find('.marked').attr('id');
console.log(marked_question);

但是,由于5天我无法找到一种方法来实现它在导航按钮。我的意思是find()可以找到类为marked的第一个div,然后如何导航到下一个类似的div。如果查找下一个div我使用

$('#at_questions_container').find('.marked').next('.marked').attr('id')

那么,如何获得下一个具有类marked的下一个?

krugob8w

krugob8w1#

你可以通过跟踪当前的id并查找下一个被标记的类来完成这一点。我已经更新了你的小提琴http://jsfiddle.net/ppt4w78y/1/

$(document).ready(
function () {
    var self = this;
    this.currentId = '1';
    this.showNext;

    $('#next').click(function () {
        ShowMarkedQuestion("next");
    });

    $('#previous').click(function () {
        ShowMarkedQuestion("previous");
    });

    function ShowMarkedQuestion(mode) {
        self.showNext = false;
        var sel = $('.question_block');
        if (mode === 'previous') {
            sel = $(sel.get().reverse());
        }

        sel.each(function(idx, obj) {

            if ($(obj).attr('id') === self.currentId || !self.currentId) {
                self.showNext = true;
            }
            else if (self.showNext && $(obj).hasClass('marked')) {
                self.showNext = false;
                $('#' + self.currentId).hide();
                self.currentId = $(obj).attr('id');
                $(obj).show();
                return false;
            }
        });
    }
});

相关问题