Changing parent Div background color on active tab

≯℡__Kan透↙ 提交于 2019-12-24 20:22:58

问题


I am trying to figure out how to change the parent div container background color when clicking a tab. I want to have it so that when a tab is active it adds a class to the parent div. Each active tab would add different bg color to the parent.

Link to the example

This is what I would like to do.


回答1:


I tried it in your console, check this

var colors = ["red", "black", "yellow"];
jQuery(document).ready(function() {
  jQuery(".vc_tta-tabs-list > li").on("click", function() { 
    jQuery(".grve-section").css("background", colors[jQuery(this).index()])
  })
})

$ is not assigned in your code, that's why I changed to jQuery here




回答2:


You can use a attribute data-color to each tab and then on click you can switch them

$(document).on('click', 'ul>li', function() {
  $(".main").css("background", $(this).data("color"));
})
body {
  font: 13px Verdana;
}

.main {
  padding: 100px 0;
}

ul {
  display: flex;
  padding: 0;
  list-style: none;
  justify-content: center;
}

ul li {
  margin: 0 10px;
  padding: 10px;
  border: 1px solid #ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
  <ul>
    <li data-color="red">red</li>
    <li data-color="blue">blue</li>
    <li data-color="cyan">cyan</li>
    <li data-color="yellow">yellow</li>
  </ul>
</div>



回答3:


add on-click event to every tab.

$(selector).click(function(){
 $(this).css("background", "red");
});


来源:https://stackoverflow.com/questions/48315026/changing-parent-div-background-color-on-active-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!