将类添加到父级

时间:2018-05-14 08:17:47

标签: jquery parent

我希望将一个元素的当前类添加到Jquery中的父div。

这是我的代码:

#sidebar { background-color:red;} /*this element change color by getting links classes*/

#contenerFAQ #sidebar.navigation ul.navigationFAQ li { 
  background-color:#fff;
  border-bottom:1px solid #f3f3f3
}

#contenerFAQ #sidebar.navigation ul.navigationFAQ li.active {
  background:transparent;
}


.navigationFAQ h3:before {
  background:url(imgs/icons-services/picto_groupe.svg);
  content: "";
  display: inline-block;
  background-size:contain; 
  background-repeat:no-repeat;
  width: 2vw;
  height: 2vw;
  top: 0px;
  position: relative;
  opacity: 0.95;
  vertical-align: middle;
  margin-left:1.5vw;
}

#contenerFAQ #sidebar .titre {
    background:#323232;
    height: 4vw;
    float: left;    
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;    
    font-size: 1vw;
    font-family:"lemonadaBold";
    line-height: 12vw;
    letter-spacing: 0.1em;
    color: #ffffff;
    text-transform: uppercase;
    margin-right:1vw;
}

.navigationFAQ {
  margin-right:1vw;
}
.navigationFAQ h3 a {
  height:3vw;
  line-height:3vw;
  padding-left:1vw;
  font-family:"dosisRegular";
  text-transform:uppercase;
  color:#333;
  text-decoration:none;
  font-size: 0.85vw;
}
<div id="sidebar" class="navigation">
  <div class="titre">
    <h2>Choisir un thème</h2>
  </div>
  <ul class="navigationFAQ">		
    <li class="methode-icon"><h3><a href="#methode-section" class="backgroundLi methodeLink">Méthode naturelle</a></h3></li>
	<li class="education-icon"><h3><a href="#education-section" class="backgroundLi educationLink">Education canine</a></h3></li>
	<li class="pension-icon "><h3><a href="#pension-section" class="backgroundLi pensionLink">Pension canine</a></h3></li>
	<li class="toilettage-icon "><h3><a href="#toilettage-section" class="backgroundLi toilettageLink">Toilettage</a></h3></li>
  </ul>
</div>

我希望将“pensionLink”,“toilettageLink”,“methodeLink”和“educationLink”添加到#sidebar。如果我们点击pensionlink,我们将“pensionLink”类添加到#sidebar等。 目标是改变#sidebar background-color,使其与链接具有相同的类。

exemple link 1 clicked exemple link 2 clicked

有人知道怎么做吗?

谢谢:)

1 个答案:

答案 0 :(得分:1)

这可以按照您的要求运作:

$(".navigationFAQ li a").click(function() {
  $("#sidebar").removeClass("methodeLink educationLink pensionLink toilettageLink").addClass($(this).attr('class').split(' ')[1])
})

<强>演示

&#13;
&#13;
$(".navigationFAQ li a").click(function() {
  $("#sidebar").removeClass("methodeLink educationLink pensionLink toilettageLink").addClass($(this).attr('class').split(' ')[1])
})
&#13;
#sidebar {
  background-color: red;
}


/*this element change color by getting links classes*/

#contenerFAQ #sidebar.navigation ul.navigationFAQ li {
  background-color: #fff;
  border-bottom: 1px solid #f3f3f3
}

#contenerFAQ #sidebar.navigation ul.navigationFAQ li.active {
  background: transparent;
}

.navigationFAQ h3:before {
  background: url(imgs/icons-services/picto_groupe.svg);
  content: "";
  display: inline-block;
  background-size: contain;
  background-repeat: no-repeat;
  width: 2vw;
  height: 2vw;
  top: 0px;
  position: relative;
  opacity: 0.95;
  vertical-align: middle;
  margin-left: 1.5vw;
}

#contenerFAQ #sidebar .titre {
  background: #323232;
  height: 4vw;
  float: left;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 1vw;
  font-family: "lemonadaBold";
  line-height: 12vw;
  letter-spacing: 0.1em;
  color: #ffffff;
  text-transform: uppercase;
  margin-right: 1vw;
}

.navigationFAQ {
  margin-right: 1vw;
}

.navigationFAQ h3 a {
  height: 3vw;
  line-height: 3vw;
  padding-left: 1vw;
  font-family: "dosisRegular";
  text-transform: uppercase;
  color: #333;
  text-decoration: none;
  font-size: 0.85vw;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sidebar" class="navigation">
  <div class="titre">
    <h2>Choisir un thème</h2>
  </div>
  <ul class="navigationFAQ">
    <li class="methode-icon">
      <h3><a href="#methode-section" class="backgroundLi methodeLink">Méthode naturelle</a></h3>
    </li>
    <li class="education-icon">
      <h3><a href="#education-section" class="backgroundLi educationLink">Education canine</a></h3>
    </li>
    <li class="pension-icon ">
      <h3><a href="#pension-section" class="backgroundLi pensionLink">Pension canine</a></h3>
    </li>
    <li class="toilettage-icon ">
      <h3><a href="#toilettage-section" class="backgroundLi toilettageLink">Toilettage</a></h3>
    </li>

  </ul>
</div>
&#13;
&#13;
&#13;