在下一个div中更改div的背景颜色

时间:2022-10-24 20:32:50

I have some divs, every div has a different background color. Based on each anchor I click some of the divs are hidden and one is displayed. My problem is that when I click on anchor with id=next that shows next div, I can't change the background color, I created a function, but it's not working, can somebody help me to fix this?

我有一些div,每个div都有不同的背景颜色。根据每个锚点我点击一些div被隐藏,一个显示。我的问题是,当我点击带有id = next的锚点显示下一个div时,我无法改变背景颜色,我创建了一个函数,但它不起作用,有人可以帮我解决这个问题吗?

My jsfiddle: DEMO

我的jsfiddle:DEMO

My HTML code:

我的HTML代码:

<body>
<div class="info-right-div" id="right-info1" style="display: block;">   
     <p> div-1- <br/> Content of div 1: color is YELLOW </p>
</div>
<div class="info-right-div" id="right-info2" style="display: none;">
     <p> div-2-  <br/> Content of div 2 : color is RED </p>
</div>
<div class="info-right-div" id="right-info3" style="display: none;"> 
     <p> div-3-  <br/> Content of div 3: color i GRAY </p>
</div>
<br /><br /> <br />
<a id="info1" href="#"> show div 1</a>
<a id="info2" href="#"> show div 2</a>
<a id="info3" href="#"> show div 3</a>
<a id="next" href="#"> next div</a>
<br /><br /> <br />

</body>

My jQuery code:

我的jQuery代码:

$(document).ready(function() {
jQuery("a").click(function() {
        var id = jQuery(this).attr("id");
        switch (true) {
        case (id == 'info1') :  
            $(".info-right-div").hide();
            $('#right-info1').show();  
            $("body").css('background-color', '#FFFF00');
            break;
        case (id == 'info2'):  
            $(".info-right-div").hide();
            $('#right-info2').show(); 
            $("body").css('background-color', '#FF0000');
            break;
       case (id == 'info3'):  
            $(".info-right-div").hide();
            $('#right-info3').show();
            $("body").css('background-color', '#00FF00');
            break;
       case(id == 'next'):
            var div1vis = $(".info-right-div").filter(':visible');
            var id = jQuery(this).attr("id");
            if (div1vis.next().length > 0) {
                div1vis.next().show().prev().hide();
                var nextid = div1vis.prev().attr("id");
                changebackground(nextid);
            }
            break;
    };
});
});


function changebackground(divid) {   
    if (divid == 'right-info1') { $("body").css('background-color', '#FFFF00'); } 
    if (divid == 'right-info1') { $("body").css('background-color', '#FF0000'); } 
    if (divid == 'right-info1') { $("body").css('background-color', '#00FF00'); } 
}

When i click on anchor with id next with var nextid = div1vis.prev().attr("id"); i tried to take id of div that is displayed.

当我点击带有id的锚时,接下来是var nextid = div1vis.prev()。attr(“id”);我试图把显示的div的id。

3 个解决方案

#1


2  

You pass undefined to changebackground if the first div is selected, since there is no div1vis.prev(). You probably need div1vis.next():

如果选择了第一个div,则将undefined传递给changebackground,因为没有div1vis.prev()。你可能需要div1vis.next():

if (div1vis.next().length > 0) {
    div1vis.next().show().prev().hide();
    // Fix:
    var nextid = div1vis.next().attr("id");
    changebackground(nextid);
}

Ids: right-info1, right-info2, right-info3:

Ids:right-info1,right-info2,right-info3:

function changebackground(divid) {
    if (divid == 'right-info1') { $("body").css('background-color', '#FFFF00'); } 
    if (divid == 'right-info2') { $("body").css('background-color', '#FF0000'); } 
    if (divid == 'right-info3') { $("body").css('background-color', '#00FF00'); } 
}

#2


3  

Check out this. This is different approach but it does the same thing https://jsfiddle.net/8k3kp6uu/1/

看看这个。这是不同的方法,但它做同样的事情https://jsfiddle.net/8k3kp6uu/1/

HTML

<div class="background-divs green active"></div>
<div class="background-divs blue"></div>
<div class="background-divs yellow"></div>

<ul class="links">
    <li><a class="link" data-background="green" href="">GREEN</a></li>
    <li><a class="link" data-background="blue" href="">BLUE</a></li>
    <li><a class="link" data-background="yellow" href="">YELLOW</a></li>
    <li><a class="next" href="link" href="">NEXT</a></li>
</ul>

JS

$('.link').click(function(e) {
    e.preventDefault();
    var color = $(this).attr('data-background');

    $('.background-divs').removeClass('active');
    $('.background-divs.'+color).addClass('active');

});

$('.next').click(function(e) {
    e.preventDefault();

    var next = $('.background-divs.active');
    var first = $('.background-divs').first();
    var last = next.last();
    next.removeClass('active').next().addClass("active");
    if(next.is(last)) {
         next.removeClass('active');
         first.addClass('active');
    }
});

#3


2  

What if you try using numeric indexes to reach the current color and id?

如果您尝试使用数字索引来获取当前颜色和ID,该怎么办?

Try this

var ids = { 1:'right-info1', 2:'right-info2', 3:'right-info3'};
// ... code of the click event here
// more code of the case control structure
   case(id == 'next'):
        if ( current >= 4 ) {current--;} else {
        $(".info-right-div").hide();
        $("#"+ids[current]).show();
        changebackground(current); }
        break;
};
// function that change colors
function changebackground(divid) {   
if (divid == 1) { $("body").css('background-color', '#FFFF00'); } 
if (divid == 2) { $("body").css('background-color', '#FF0000'); } 
if (divid == 3) { $("body").css('background-color', '#00FF00'); } }

You can find the complete code here https://jsfiddle.net/1yscy5ts/13/

你可以在这里找到完整的代码https://jsfiddle.net/1yscy5ts/13/

#1


2  

You pass undefined to changebackground if the first div is selected, since there is no div1vis.prev(). You probably need div1vis.next():

如果选择了第一个div,则将undefined传递给changebackground,因为没有div1vis.prev()。你可能需要div1vis.next():

if (div1vis.next().length > 0) {
    div1vis.next().show().prev().hide();
    // Fix:
    var nextid = div1vis.next().attr("id");
    changebackground(nextid);
}

Ids: right-info1, right-info2, right-info3:

Ids:right-info1,right-info2,right-info3:

function changebackground(divid) {
    if (divid == 'right-info1') { $("body").css('background-color', '#FFFF00'); } 
    if (divid == 'right-info2') { $("body").css('background-color', '#FF0000'); } 
    if (divid == 'right-info3') { $("body").css('background-color', '#00FF00'); } 
}

#2


3  

Check out this. This is different approach but it does the same thing https://jsfiddle.net/8k3kp6uu/1/

看看这个。这是不同的方法,但它做同样的事情https://jsfiddle.net/8k3kp6uu/1/

HTML

<div class="background-divs green active"></div>
<div class="background-divs blue"></div>
<div class="background-divs yellow"></div>

<ul class="links">
    <li><a class="link" data-background="green" href="">GREEN</a></li>
    <li><a class="link" data-background="blue" href="">BLUE</a></li>
    <li><a class="link" data-background="yellow" href="">YELLOW</a></li>
    <li><a class="next" href="link" href="">NEXT</a></li>
</ul>

JS

$('.link').click(function(e) {
    e.preventDefault();
    var color = $(this).attr('data-background');

    $('.background-divs').removeClass('active');
    $('.background-divs.'+color).addClass('active');

});

$('.next').click(function(e) {
    e.preventDefault();

    var next = $('.background-divs.active');
    var first = $('.background-divs').first();
    var last = next.last();
    next.removeClass('active').next().addClass("active");
    if(next.is(last)) {
         next.removeClass('active');
         first.addClass('active');
    }
});

#3


2  

What if you try using numeric indexes to reach the current color and id?

如果您尝试使用数字索引来获取当前颜色和ID,该怎么办?

Try this

var ids = { 1:'right-info1', 2:'right-info2', 3:'right-info3'};
// ... code of the click event here
// more code of the case control structure
   case(id == 'next'):
        if ( current >= 4 ) {current--;} else {
        $(".info-right-div").hide();
        $("#"+ids[current]).show();
        changebackground(current); }
        break;
};
// function that change colors
function changebackground(divid) {   
if (divid == 1) { $("body").css('background-color', '#FFFF00'); } 
if (divid == 2) { $("body").css('background-color', '#FF0000'); } 
if (divid == 3) { $("body").css('background-color', '#00FF00'); } }

You can find the complete code here https://jsfiddle.net/1yscy5ts/13/

你可以在这里找到完整的代码https://jsfiddle.net/1yscy5ts/13/