简体   繁体   English

更改DIV上的背景颜色-使用标签交换颜色

[英]Changing background color on DIV - Swap color with tabs

I'm trying to change the background color of three divs when they are clicked. 我正在尝试更改单击三个div的背景颜色。 I have managed to change the background color and content of the div when it is clicked but can't work out how to return the div to the original state? 单击时,我设法更改了div的背景颜色和内容,但无法解决如何将div返回到原始状态吗?

It should function as three tabs along the top (one,two,three) with one by default green, when any of the others are clicked the defaut changes and the one that was clicked changes. 它应沿顶部(一个,两个,三个)作为三个选项卡使用,默认情况下为绿色,当单击其他任何一个时,默认更改将更改,而单击的则更改。 http://jsfiddle.net/0es6neph/ http://jsfiddle.net/0es6neph/

Tab One should be selected by default. 默认情况下应选择选项卡一。

Am I going the right way about this or is it a messy solution to a straightforward problem ? 我是否对此采取了正确的方法,还是对一个简单问题的一种混乱解决方案?

<script type="text/javascript">
var currentDiv = null;

function swapin(divName){
if(currentDiv != null){
document.getElementById(currentDiv).style.display = "none";

}
if(document.getElementById != 'contain'){
document.getElementById('1').style.display = "none";
}
currentDiv = divName;
document.getElementById(currentDiv).style.display = "block";
}
window.onload = function(){
document.getElementById('1').style.display = "block";
}

</script>

<style>
.tabs {
    background-color:#7D135A;
    border-radius: 2px 2px 0 0;
    height: 50px;
    width: 90px;
    padding-left:10px;
    padding-right:10px;
    padding-top:5px;
    padding-bottom:0px;
    color:#FFFFFF;
    }

 .contain {
    width:500px;
    height:200px;
    border:2px;
    border-style: solid;
    border-color: #7D135A;
    }



</style>

<a href="javascript: swapin('1');" class="tabs">One</a>&nbsp;
<a href="javascript: swapin('2');" class="tabs" >Two</a>&nbsp;
<a href="javascript: swapin('3');" class="tabs">Three</a>&nbsp;

<div id="contain" class="contain">
<div id="1" style="display: none;">
One Content
</div>
<div id="2" style="display: none;">
Two Content
</div>
<div id="3" style="display: none;">
Three Content
</div>
</div>

<script>
$(function () {
$(".tabs").click(function () {
    $(this).css('background-color', '#008000');
});
});
</script>

That's really messy ... I detected some unnecessary routes in your code: 真的很乱。我在您的代码中检测到一些不必要的路由:

1) Use ids starting with a letter like #tab1 and never numbers alone. 1)使用以#tab1之类的字母开头的ID,不要单独使用数字。 2) No need to encapsulate this process in a function. 2)无需将此过程封装在一个函数中。 This is a straightforward procedure that doesn't need to be addressed like a repetitive pattern. 这是一个简单的过程,不需要像重复模式那样进行处理。 3) Do not call the function inside the href attribute. 3)不要在href属性内调用该函数。 Use onclick to run JS click instructions instead. 请使用onclick来运行JS单击说明。

Use this approach instead: JSFIDDLE 请改用这种方法: JSFIDDLE

$(function () {
    $(".tab").click(function () {
        var tab = $(this),
            dataTab = tab.attr('data-tab');
        tab.addClass('active').siblings('.tab').removeClass('active');
        $('#'+dataTab).show().siblings().hide();
    });
});

I would recommend using a class for changing the state of your links. 我建议使用一个类来更改链接的状态。

Add this in css 在css中添加

.active{
    background-color: #008800;
}

Change your click function to this 将点击功能更改为此

$(".tabs").click(function () {
        $(".active").removeClass("active");
        $(this).addClass('active');
    });

Check it out here in JSFiddle JSFiddle查看

我想你应该给那些div的一类如: .bg然后你应该在jQuery的改变背景颜色一样here

Use jQuery as you have already load it. 使用jQuery,因为您已经加载了它。 And try not to mesh up your code. 并尽量不要使您的代码网格化。

 var openTab = null; function swapin(newTab){ if(openTab != null){ openTab.css('background-color','#7D135A'); $(openTab.data('tab-element')).css('display',"none"); } newTab.css('background-color','#008000'); $(newTab.data('tab-element')).css('display',"block"); openTab = newTab; } $(function () { $(".tabs .tab").click(function () { swapin($(this)); }); }); 
 .tabs .tab{ background-color: #7D135A; border-radius: 2px 2px 0 0; height: 50px; width: 90px; padding-left: 10px; padding-right: 10px; padding-top: 5px; padding-bottom: 0px; color: #FFFFFF; cursor: pointer; } .contain { width: 500px; height: 200px; border: 1px; border-style: solid; border-color: #7D135A; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tabs"> <a click="swapin()" class="tab" data-tab-element="#1">One</a> <a click="swapin()" class="tab" data-tab-element="#2">Two</a> <a click="swapin()" class="tab" data-tab-element="#3">Three</a> </div> <div id="contain" class="contain"> <div id="1" style="display: none;"> One Content </div> <div id="2" style="display: none;"> Two Content </div> <div id="3" style="display: none;"> Three Content </div> </div> 

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

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