简体   繁体   English

如何使用 JavaScript 使 Css class 消失并出现

[英]How to make a Css class disappear and appear using JavaScript

so I have this CSS:所以我有这个 CSS:

 .cormes{ font-size: 30px; margin-bottom: 10px; text-align: centre; }

I need to make that disappear and appear using javascript.我需要使用 javascript 让它消失并出现。 I know you can use style.display='none' and style.display='inline-block' for normal ones.我知道您可以将 style.display='none' 和 style.display='inline-block' 用于普通的。 I don't know how to do this for classes.我不知道如何在课堂上做到这一点。 Please help请帮忙

If you want to affect all elements with a class you have to loop in them:如果你想用 class 影响所有元素,你必须循环它们:

var elems = document.getElementsByClassName('cormes');
for (var i = 0; i < elems.length; i++ ) {
    elems[i].style.display = "none";
}

More info about this here: https://stackoverflow.com/a/20946374/7919626有关此的更多信息: https://stackoverflow.com/a/20946374/7919626

either you assign a class which is invisible or you add a new style to a existing class.要么分配不可见的 class,要么向现有 class 添加新样式。 Usually its not a good idea to redefine css classes due side effects.由于副作用,重新定义 css 类通常不是一个好主意。 For the sake of showing you something new i use the visibility css rule.为了向您展示新的东西,我使用了可见性 css 规则。 Display: "none" would do the same.显示:“无”会做同样的事情。 This new class i would add either hard coded in the css or by js.这个新的 class 我将在 css 或通过 js 添加硬编码。

 .cormes_invisible{visibility:hidden;}

Now all you has to do is to simply change the class name.现在您只需更改 class 名称即可。 I add "_invisible" so its rather trivial to restore the original class later one by using the split function.我添加了“_invisible”,因此稍后使用拆分 function 恢复原始 class 相当简单。 So there is nothing complicated or strange in it:)所以没有什么复杂或奇怪的:)

      function makeinvisible(id){
       var element=document.getElementById(id)
        try { // rather brutal way
          element.classList.remove("cormes");
          } catch (ex){}
          element.classList.add("cormes_invisible");
   }    

        function makevisible(id){
       var element=document.getElementById(id)
        try { // rather brutal way
          element.classList.remove("cormes_invisible");
          } catch (ex){}
          element.classList.add("cormes");
      }

another way would be to add a style attribute with the invisible css.另一种方法是使用不可见的 css 添加样式属性。 and remove it afterwards.然后将其删除。 There are a few posiibilitys.有几个可能性。 (And alot of very nice tutorials in the web;)) (在 web 中有很多非常好的教程;))

btw: the other classic way would be:顺便说一句:另一种经典方式是:

      var elements =document.getElementsByClassName("cormes");

      for (i=0;i<elements.length;i++){
       try { // rather brutal way
          elements[i].classList.remove("cormes");
          } catch (ex){}
          elements[i].classList.add("cormes_invisible");
      }

First get the classes wrapped in the HTMLCollection , and then hide each one with with a forEach首先获取包装在HTMLCollection中的类,然后用forEach隐藏每个类

const elements = document.getElementsByClassName('cormes');
Array.prototype.forEach.call(elements, element => element.style.display = 'none');

The same with ...display = 'initial' , if we want to show them again....display = 'initial'相同,如果我们想再次显示它们。 Would be useful to create a function for easily show and hide the cormes.用于创建 function 以轻松显示和隐藏角将很有用。

function showCormes(show) {
  const styleValue = show ? 'initial' : 'none';
  Array.prototype.forEach.call(elements, element => element.style.display = styleValue);
}

So we can easily show and hide that way:所以我们可以很容易地显示和隐藏这种方式:

showCormes(true);
showCormes(false);

If you want to remove CSS class you can do it by using Javascript remove method如果要删除 CSS class 可以使用 Javascript 删除方法来完成

For example:-例如:-

let element = document.getElementById('demo');
element.classList.remove('myClass');

HTML Code for above example:-上述示例的 HTML 代码:-

<div id="demo" class="myClass"></div>

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

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