简体   繁体   English

元素上的Javascript切换类

[英]Javascript toggle class on element

Say I have this HTML structure 说我有这个HTML结构

<div class="sType_click">
    <div class="Switcher">
      <span class="customText">Custom text</span>
    </div>
</div>

<div class="sType_click">
    <div class="Switcher">
      <span class="customText">Custom text</span>
    </div>
</div>

<div class="sType_click">
    <div class="Switcher">
      <span class="customText">Custom text</span>
    </div>
</div>

How do I use JavaScript to toggle class on sType_click div with the customText span BUT without adding it on other sType_click divs? 如何使用JavaScript在customText span BUT上切换sType_click div上的类,而不在其他sType_click div上添加它?

For example if I click on one of the customText class I want to toggle class on its parent sType_click div and not adding on any others. 例如,如果我单击customText类之一,则想在其父级sType_click div上切换类,而不添加任何其他类。

I currently use this 我目前正在使用

var sType_click = document.querySelector(".sType_click"),
customText = document.querySelector(".customText"),
if (null !== document.querySelector(".customText")) {
    customText.onclick = function() {
        sType_click.classList.toggle("sOpen");
    };
}

But this toggles all sType_click divs on the page I'm okay with jQuery but i prefer pure javascript, Thanks in advance. 但这会切换页面上的所有sType_click div,我可以使用jQuery,但我更喜欢纯JavaScript,谢谢。

Pure JS solution: 纯JS解决方案:

You should listen for clicks on customText elements and get 2 levels higher using parentNode twice. 您应该侦听对customText元素的单击,并使用parentNode两次将其提高2级。 Also, use querySelectorAll to get all elements (non-live list, so it doesn't update when you toggle class). 另外,使用querySelectorAll获取所有元素(非活动列表,因此在切换类时它不会更新)。

 var fwcustomText = document.querySelectorAll(".sType_click .customText"); for (var ct of fwcustomText) { // listen to clicks on ".customText" elements ct.addEventListener('click', function(event) { event.target.parentNode.parentNode.classList.toggle("sType_click"); // toggle class 2 levels higher }) } 
 .sType_click { background-color: red; } 
 <div class="sType_click"> <div class="Switcher"> <span class="customText">Custom text</span> </div> </div> <div class="sType_click"> <div class="Switcher"> <span class="customText">Custom text</span> </div> </div> <div class="sType_click"> <div class="Switcher"> <span class="customText">Custom text</span> </div> </div> 

Or you can make it with jquery so much easier: 或者,您也可以使用jquery轻松得多:

 $('.fw_customText').on('click', function() { $(this).closest(".sType_click").toggleClass("sOpen"); }); 
 .sOpen{ background-color:green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="sType_click"> <div class="Switcher"> <span class="fw_customText">Custom text</span> </div> </div> <div class="sType_click"> <div class="Switcher"> <span class="fw_customText">Custom text</span> </div> </div> <div class="sType_click"> <div class="Switcher"> <span class="fw_customText">Custom text</span> </div> </div> 

its easy, Just add an event click to the object 这很容易,只需将事件添加到对象

es: ES:

<div class="sType_click" onclick="sType_click_eve(this)">
    <div class="Switcher">
    <span class="fw_customText">Custom text</span>
</div>

And on the page 然后在页面上

function sType_click_eve(ele){
      var sType_click = ele,
      customText = document.querySelector(".customText"),
      if (null !== document.querySelector(".customText")) {
         customText.onclick = function() {
            sType_click.classList.toggle("sOpen");
         };
      } 
}

I think you use jQuery because it requires less code as compare to javascript. 我认为您使用jQuery是因为与JavaScript相比,它需要的代码更少。

Check below snippet: 检查以下代码段:

 $(document).on('click', '.customText', function() { $(this).closest(".sType_click").toggleClass("selected"); }); 
 .selected { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="sType_click"> <div class="Switcher"> <span class="customText">Custom text</span> </div> </div> <div class="sType_click"> <div class="Switcher"> <span class="customText">Custom text</span> </div> </div> <div class="sType_click"> <div class="Switcher"> <span class="customText">Custom text</span> </div> </div> 

You can use simple JS to do that getting all the elements and accessing the parent element you want to: 您可以使用简单的JS来获取所有元素并访问您想要的父元素:

 Array.from(document.querySelectorAll(".fw_customText")).forEach((el) => { el.addEventListener('click', (event) => { event.currentTarget.parentElement.parentElement.classList.toggle("sType_click"); }); }); 
 .sType_click{ background-color: yellow; } 
 <div class="sType_click"> <div class="Switcher"> <span class="fw_customText">Custom text</span> </div> </div> <div class="sType_click"> <div class="Switcher"> <span class="fw_customText ">Custom text</span> </div> </div> <div class="sType_click"> <div class="Switcher"> <span class="customText">Custom text</span> </div> </div> 

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

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