简体   繁体   English

查找下一个/上一个元素

[英]Find Next / Previous Element

I am loading data via ajax from a row of divs with a class of .gameListing. 我正在通过ajax从具有类.gameListing的div行中加载数据。 When one of the divs is clicked it has a class of .active added and loads the relative data into a container, this is all working perfectly. 单击其中一个div时,将添加一类.active并将相关数据加载到容器中,这一切都可以正常工作。

What I would like is to have a Previous & Next link which finds the next or prev .gameListing and clicks it. 我想要的是一个上一个和下一个链接,用于找到下一个或上一个.gameListing并单击它。

$('.gameListing').click(function(){

    $('.gameListing').removeClass('active');
    $(this).addClass('active');

    var id = $(this).attr('data-id');
    var url = "http://localhost:8888/projects/superfreerespo/" + id + "?json=get_category_posts&slug=games";

    call_ajax(url);
});




function call_ajax(url) {

    $.ajax( {
            url: url,
            method: "GET",
            data: {json:  1},
            dataType: "JSON"
     })

        .done(function( data ) {

        // LOAD GAME INFORMATION
        $("#game-name").html(data.post.title);
        $("#game-reels").html(data.post.custom_fields.reels);
        $("#game-paylines").html(data.post.custom_fields.paylines);
        $("#game-minBet").html(data.post.custom_fields.min_bet);
        $("#game-maxBet").html(data.post.custom_fields.max_bet);
        $("#game-jackpot").html(data.post.custom_fields.jackpot);
        $("#game-info").html(data.post.custom_fields.game_info);

        // LOAD GAME PROVIDERS
        var provSource = new String(data.post.custom_fields.game_name);
                provSource = provSource.replace(/ /g,"-");
                $("#game_provs").load("http://localhost:8888/projects/superfreerespo/" + provSource + "/ .gameBox-Ops");

        // LOAD GAME THUMBNALS
        var gameThumbSrc = new String(data.post.custom_fields.game_name);
        gameThumbSrc = gameThumbSrc.replace(/ /g,'');

        $('#gameBoxGallery').html('');
            for(i = 0;  i<= 2; i++){
                            image = '<img src="<?php bloginfo('template_directory'); ?>/images/games/screenshots/' + gameThumbSrc + '-' + i + '.jpg" class="gameThumb">'
                            $('#gameBoxGallery').append(image);
        };

        // ZOOM FIRST THUMBNAIL
        $('#gameBox-Screenshot').html('');
            image = '<img src="<?php bloginfo('template_directory'); ?>/images/games/screenshots/' + gameThumbSrc + '-0' + '.jpg" id="gameScreenshot">'
        $('#gameBox-Screenshot').append(image);

        })  
}

How about something like this: 这样的事情怎么样:

$('.prev').click(function(){

    var $current = $('.gameListing .active');
    $current.removeClass('active')
    $current.prev().addClass('active');

    var id = $current.prev().attr('data-id');
    var url = "http://localhost:8888/projects/superfreerespo/" + id + "?json=get_category_posts&slug=games";

    call_ajax(url);
});

$('.next').click(function(){

    var $current = $('.gameListing .active');
    $current.removeClass('active')
    $current.next().addClass('active');

    var id = $current.next().attr('data-id');
    var url = "http://localhost:8888/projects/superfreerespo/" + id + "?json=get_category_posts&slug=games";

    call_ajax(url);
});

UPDATE 更新

If you want to use a generic function: 如果要使用通用函数:

$('.prev').click(function(){
     Navigate('previous');
});

$('.next').click(function(){
     Navigate('next');
});


function Navigate (direction){
    var $current = $('.gameListing .active');
    $current.removeClass('active')

    var secondItem = $current.next().addClass('active');
    if(direction == 'previous')
         secondItem = $current.prev().addClass('active');

    var id = secondItem.attr('data-id');
    var url = "http://localhost:8888/projects/superfreerespo/" + id + "?json=get_category_posts&slug=games";

    call_ajax(url);

}

You can use .next() from Jquery 您可以从Jquery使用.next()

for more detail https://api.jquery.com/next/ & for Previous use .prev() https://api.jquery.com/prev/ 以获得更多详细信息https://api.jquery.com/next/&以前使用.prev() https://api.jquery.com/prev/

Here's a solution for if your .gameListing are not next to eachother (so if next() and prev() won't work): 如果您的.gameListing彼此不相邻,这是一个解决方案(因此,如果next()prev()无法使用):

Fiddle for if the code snippet doesn't work. 弄弄代码段是否不起作用。

 jQuery.fn.reverse = function() { return this.pushStack(this.get().reverse(), arguments); }; function GameListing(el) { $('.gameListing').removeClass('active'); $(el).addClass('active'); var id = $(el).attr('data-id'); var url = "http://localhost:8888/projects/superfreerespo/" + id + "?json=get_category_posts&slug=games"; //call_ajax(url); $('#result').text('URL: ' + url); } $('.gameListing').click(function () { GameListing(this); }); $('.prev').click(function () { var next = false; $('.gameListing').reverse().each(function () { if (next) { GameListing(this); return false; } if ($(this).hasClass('active')) { next = true; } }); }); $('.next').click(function () { var next = false; $('.gameListing').each(function () { if (next) { GameListing(this); return false; } if ($(this).hasClass('active')) { next = true; } }); }); function call_ajax(url) { // Your code here } 
 div { display:inline-block; border:1px solid black; padding:2px 5px; margin:0 5px; cursor:pointer; float:left; } div.active { background:green; } #result { background:red; display:block; float:left; clear:left; width:300px; text-align:center; margin:20px 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="prev">&laquo;</div> <div class="gameListing" data-id="1">1</div> <div class="gameListing" data-id="2">2</div> <div class="gameListing" data-id="3">3</div> <div class="gameListing" data-id="4">4</div> <div class="next">&raquo;</div> <div id="result"></div> 

Simple solution using custom data attribute 使用自定义数据属性的简单解决方案

var $current = $('.gameListing.active');
$current.removeClass('active')

var postNumber = parseInt($current.attr('data-count'));

var nextPost = (postNumber - 1);

$("[data-count='"+nextPost+"']").trigger("click");

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

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