【问题标题】:JS Apply custom style on click on a div and remove styling from the other oneJS 在单击一个 div 时应用自定义样式并从另一个中删除样式
【发布时间】:2019-01-07 20:51:32
【问题描述】:

我有 2 个 50% 宽度的开关,点击它会显示 100% 以下的 div 并隐藏另一个,反之亦然。

使这成为可能的功能工作正常,但我想做的是在显示的切换上添加自定义样式

最后应该是这样的: The div one is displayed automatically with the custom style border black, thickker width bottom and text color black

这是现在的样子:This is the style i would like when not openend, but i would like to change as i said the border and color style

`

function switch_div(show) {  
  document.getElementById("show_"+show).style.display="block";
  document.getElementById("show_"+((show==1)?2:1)).style.display = "none";
}

function stylish(){
document.getElementById("borderRight").setAttribute(
   "style", "color:#000000;border:1px solid black;border-bottom:5px solid black;");
  
  document.getElementById("buttonRight").style.color = "#000000";
}
   function stylish2(){
document.getElementById("borderLeft").setAttribute(
   "style", "color:#000000;border:1px solid black;border-bottom:5px solid black;");
       document.getElementById("buttonLeft").style.color = "#000000";
   }
.content {
  width: 100%;
  height: 100px;
  display: block;
  background-color:#fafafa;
  margin-top:3%;
}
  

.hide {
  display: none;
}

  .align1{
    display:inline-block;
    width:49%;
    height:50px;
    text-align:center;
    border:1px solid #c3c6c6;
   cursor:pointer;
    
  }
  #borderRight{
    border-right:0px;
  }
  .titres{
    font-family:Roboto;
    font-size:20px;
    background-color:transparent;
    border:0px;
     padding-top:2.3%;
    font-weight:700;
    color:#a2a2a4;
    text-transform:uppercase;
    font-size:20px;
    letter-spacing:1.5px;
   cursor:pointer;
    
  }

  .titres:hover{
    color:#000000;  
  }
<div id="borderRight" class="align1" onclick="switch_div(1);">
          <button id="buttonRight" class="titres" onclick="stylish()">Description</button>
    </div><div id="borderLeft"class="align1"onclick="switch_div(2);"><button id="buttonLeft" class="titres" onclick="stylish2()">
          Additional information</button>
					</div>

<div class="content" id="show_1">
  Show by default (and when button 1 is clicked)
</div>

<div class="content hide" id="show_2">
  Div number 2
</div>

`

感谢您的帮助!

【问题讨论】:

  • 阿联酋在 css 上的 active 或 fucus 方法很容易
  • @Dario 添加了anwser,请查看

标签: javascript html css


【解决方案1】:

谢谢,很好!我只是调整文字颜色,但结果很好,花了我几个小时才找到方法,非常感谢!祝你有美好的一天!

【讨论】:

  • 很高兴为您提供帮助,祝您有美好的一天)
【解决方案2】:

希望这会有所帮助:

document.addEventListener("DOMContentLoaded", function() {
  stylish();
});

function switch_div(show) {  
  document.getElementById("show_"+show).style.display="block";
  document.getElementById("show_"+((show==1)?2:1)).style.display = "none";
}

function stylish(){
document.getElementById("borderLeft").removeAttribute('style');
document.getElementById("borderRight").setAttribute(
   "style", "color:#000000;border:1px solid black;border-bottom:5px solid black;");
  
  document.getElementById("buttonRight").style.color = "#000000";
}
   function stylish2(){
   document.getElementById("borderRight").removeAttribute('style');
document.getElementById("borderLeft").setAttribute(
   "style", "color:#000000;border:1px solid black;border-bottom:5px solid black;");
       document.getElementById("buttonLeft").style.color = "#000000";
   }
.content {
  width: 100%;
  height: 100px;
  display: block;
  background-color:#fafafa;
  margin-top:3%;
}
  

.hide {
  display: none;
}

  .align1{
    display:inline-block;
    width:49%;
    height:50px;
    text-align:center;
    border:1px solid #c3c6c6;
   cursor:pointer;
    
  }
  #borderRight{
    border-right:0px;
  }
  .titres{
    font-family:Roboto;
    font-size:20px;
    background-color:transparent;
    border:0px;
     padding-top:2.3%;
    font-weight:700;
    color:#a2a2a4;
    text-transform:uppercase;
    font-size:20px;
    letter-spacing:1.5px;
   cursor:pointer;
    
  }

  .titres:hover{
    color:#000000;  
  }
<div id="borderRight" class="align1" onclick="switch_div(1);">
          <button id="buttonRight" class="titres" onclick="stylish()">Description</button>
    </div><div id="borderLeft"class="align1"onclick="switch_div(2);"><button id="buttonLeft" class="titres" onclick="stylish2()">
          Additional information</button>
					</div>

<div class="content" id="show_1">
  Show by default (and when button 1 is clicked)
</div>

<div class="content hide" id="show_2">
  Div number 2
</div>

【讨论】:

    猜你喜欢
    • 2023-02-12
    • 2016-10-01
    • 2015-11-08
    • 2017-01-13
    • 2014-04-23
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多