Changing parent Div background color on active tab

2019-08-22 06:14发布

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.

enter image description here

3条回答
我想做一个坏孩纸
2楼-- · 2019-08-22 06:44

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

查看更多
唯我独甜
3楼-- · 2019-08-22 06:45

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>

查看更多
成全新的幸福
4楼-- · 2019-08-22 06:51

add on-click event to every tab.

$(selector).click(function(){
 $(this).css("background", "red");
});
查看更多
登录 后发表回答