简体   繁体   English

从JS中的CSS类中删除display:none

[英]remove display:none from a css class in JS

I've made this list of GoT charachters that could possible die (there's no spoilers or what so ever). 我已经列出了可能会死亡的GoT角色(没有剧透,也从未如此)。 The problem I'm having is that I'm supposed to remove a class but I can't make it work. 我遇到的问题是我应该删除一个类,但无法使其正常工作。 I could just remove the CSS, I know but that's not what I'm trying to achieve here. 我知道我可以删除CSS,但这不是我要在这里实现的目标。 I can't find the way to delete a CSS class during a JS event/function. 我找不到在JS事件/功能期间删除CSS类的方法。

I can make it work by deleting the "li" tags so that part I've completed. 我可以通过删除“ li”标签使其工作,从而完成我的部分。

My HTML: 我的HTML:

<ul class="carousel">
   <!-- <li><img src="images/cersei-lannister-1920.jpg" alt="Cersei Lannister" title="Cersei Lannister"/></li>
    <li><img src="images/daenarys-1920.jpg" alt="Daenarys" title="Daenarys"/></li>
    <li><img src="images/maetser-varys-1920.jpg" alt="Maester Varys" title="Maester Varys"/></li>-->
</ul>

MY JS: 我的JS:

function changeImageIndex(value){
    let charachter =["images/cersei-lannister-1920.jpg","images/daenarys-targaryen-1920.jpg", "images/maester-varys-1920.jpg", "images/margarey-tyrell-1920.jpg", "images/petyr-baelish-1920.jpg", "images/samwell-tarly-1920.jpg", "images/sansa-stark-1920.jpg"];


    let str = "<li><img src='";


    if(value === "1")
    {
        parseInt(i += 1);
        if( i > 6)
        {
            i = 0
        }
        str += charachter[i];

        console.log(str);
    }

    else{
        parseInt(i -= 1);
        if( i < 0)
        {
            i = 6;
        }

        str += charachter[i];
        console.log(str);
    }




    str += "'></img></li>";

    console.log(str);


    let HTML = document.getElementsByClassName("carousel")[0];
    console.log(document.querySelector("ul li").classList.remove("carousel"));

    document.querySelector("li").classList.remove("carousel");


    HTML.innerHTML = str;


}

MY CSS that I want gone: 我想要的CSS:

.carousel li {
  display: none;
}

I hope you guys can help me out here! 我希望你们能在这里帮助我! Thanks! 谢谢!

Make a seperate class take ".hidecarousel" for display:none and add it to your CSS. 使一个单独的类使用".hidecarousel" for display:none ,并将其添加到CSS中。 Use the class name in your li tags. 在您的li标签中使用类名。 And then make use of the javascript to simply remove it. 然后利用javascript简单地将其删除。

var list = document.getElementsByClassName("hidecarousel");
while (list.length)
    list[0].classList.remove("hidecarousel");

Here is how you can change the CSS of all the li inside your carousel ul to block. 您可以通过以下方法更改轮播ul中所有li的CSS以进行阻止。

 var array_of_li = document.querySelectorAll("ul.carousel li"); var i; for (i = 0; i < array_of_li.length; ++i) { array_of_li[i].style.display = "block"; } 
 .carousel li { display: none; } 
 <ul class="carousel"> <li>First</li> <li>Second</li> <li>Third</li> </ul> 

据我了解,您没有使用jQuery,因此这是用于从元素中删除类的Javascript解决方案。

ELEMENT.classList.remove("CLASS_NAME");

Maybe if you add a class by js and in CSS you do what you want 也许如果您通过js添加一个类并在CSS中执行了您想要的操作

.carousel li.active{display:inline-block;}

Hope you can make it :) 希望你能做到 :)

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

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