简体   繁体   English

Vanilla JS(无 jQuery) - 如何一次切换多个类?

[英]Vanilla JS (no jQuery) - How to toggle more than one class at once?

I am dealing with a legacy system and I can't rewrite the whole css.我正在处理一个遗留系统,我无法重写整个 css。

So I find myself in need to toggle 2 classes on the same element when another one is clicked (imagine clicking on a button to hide/show a modal).因此,当单击另一个类时,我发现自己需要在同一元素上切换 2 个类(想象一下单击按钮以隐藏/显示模态)。

The following is the code for the modal.以下是模态的代码。
Where the modal window is by default .hidden (without the class "visible") and on click it is .visible (without the class "hidden").凡模态窗口在默认情况下.hidden (无类“可见”),并点击它.visible (无类“隐藏”)。

 function openModal(modalID) { document.getElementById(modalID) .classList.toggle('hidden'); document.getElementById(modalID) .classList.toggle('visible'); }
 I agree to accept the <a href="#" onclick="openModal('tandcmodal')"> terms and conditions </a> of this website. <div id="tandcmodal" class="hidden"> Content of the modal </div>

So, I believe there must be a way to toggle more than one class at once with .toggle() .因此,我相信必须有一种方法可以使用.toggle()一次切换多个类。
Or isn't it?或者不是吗?

Unfortunately, .toggle accepts only one argument : the one class name you wish to toggle.不幸的是, .toggle 只接受一个参数:您希望切换的一个类名。 To be DRY, you'll have to iterate over an array of class names, or save the classList in a variable and call .toggle twice:为了 DRY,你必须遍历一个类名数组,或者将 classList 保存在一个变量中并调用.toggle两次:

 function openModal(modalID) { const { classList } = document.getElementById(modalID); classList.toggle('hidden'); classList.toggle('visible'); }
 .hidden { display: none; } .visible { display: block; }
 I agree to accept the <a href="#" onclick="openModal('tandcmodal')"> terms and conditions </a> of this website. <div id="tandcmodal" class="hidden"> Content of the modal </div>

But note that usually there's no need for both a hidden and a visible class.但请注意,通常不需要hidden类和visible类。 Often you can have just a hidden class, and add it / remove it, letting the default display style take effect when hidden is not active:通常,您可以拥有一个hidden类,然后添加/删除它,让默认显示样式在hidden未处于活动状态时生效:

 function openModal(modalID) { document.getElementById(modalID).classList.toggle('hidden'); }
 .hidden { display: none; }
 I agree to accept the <a href="#" onclick="openModal('tandcmodal')"> terms and conditions </a> of this website. <div id="tandcmodal" class="hidden"> Content of the modal </div>

Also note that inline attribute handlers behave very strangely, have escaping problems, and are generally considered to be quite poor practice - they should probably be avoided in almost all cases.另请注意,内联属性处理程序的行为非常奇怪,存在转义问题,并且通常被认为是非常糟糕的做法 - 在几乎所有情况下都应该避免使用它们。 Consider adding the event listener using Javascript instead:考虑使用 Javascript 添加事件侦听器:

 document.querySelector('a').addEventListener('click', () => openModal('tandcmodal')); function openModal(modalID) { document.getElementById(modalID).classList.toggle('hidden'); }
 .hidden { display: none; }
 I agree to accept the <a href="#"> terms and conditions </a> of this website. <div id="tandcmodal" class="hidden"> Content of the modal </div>

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

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