[英]Change background color of divs in next div click
我有一些div,每个div都有不同的背景颜色。 根据每个锚点我点击一些div被隐藏,一个显示。 我的问题是,当我点击带有id=next
锚点显示下一个div时,我无法改变背景颜色,我创建了一个函数,但它不起作用,有人可以帮我解决这个问题吗?
我的jsfiddle: DEMO
我的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>
我的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'); }
}
当我点击带有id的锚时, next
用var nextid = div1vis.prev().attr("id");
我试图把显示的div的id。
看看这个。 这是不同的方法,但它做同样的事情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');
}
});
如果选择了第一个div
, changebackground
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
right-info2
:
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'); }
}
如果您尝试使用数字索引来获取当前颜色和ID,该怎么办?
尝试这个
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'); } }
你可以在这里找到完整的代码https://jsfiddle.net/1yscy5ts/13/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.