简体   繁体   English

JavaScript覆盖CSS显示:无可见隐藏

[英]javascript override css display: none visible hidden

I have a CSS id that both hides visibility and displays: none. 我有一个CSS ID,它既隐藏可见性又显示:没有。 When I click the button I would like for it to display. 当我单击按钮时,我希望它显示。 However, the only way I am able to make it work is by removing the display:none. 但是,我唯一能使它工作的方法是删除display:none。 I do not want this because it is an invisible element causing design to be messed up. 我不希望这样做,因为它是导致设计混乱的不可见元素。

Ultimately the goal is to click button, make some elements disappear and some elements appear. 最终目标是单击按钮,使某些元素消失而某些元素出现。

Here is the HTML 这是HTML

<div class="fwork1" id="fwork1">

    <a href="#portfolio" onclick="fWork1()">
        <img src="assets/img/portfolio/corinthmc/corinthmc_small.png" alt=""> 
    </a>

</div>

<div id="hwork1">

    <p>some text</p>

</div>

<div id="fWorkReturn">

    <button onclick="fWorkReturn()">Click</button>

</div>

Here is the CSS 这是CSS

#hwork1 {
    visibility: hidden;
    display: none;
}

Here is the Javascript 这是Javascript

function fWork1() {
    document.getElementById("fwork2").style.display = "none";
    document.getElementById("fwork3").style.display = "none";
    document.getElementById("fwork4").style.display = "none";
    document.getElementById("hwork1").style.display = "block"; 
}

function fWorkReturn() {
    document.getElementById("fwork1").style.display = "initial";
    document.getElementById("fwork2").style.display = "initial";
    document.getElementById("fwork3").style.display = "initial";
    document.getElementById("fwork4").style.display = "initial";
    document.getElementById("hwork1").style.visibility = "hidden";
}

JSFiddle JSFiddle

The best way to handle this would be to add a class when you click on you element. 解决此问题的最佳方法是在单击元素时添加一个类。 your js would look like this: 您的js看起来像这样:

function fWork1() {
    document.getElementById("fwork2").classList().add('active');
}

and you CSS will look like this: 并且您的CSS将如下所示:

#hwork1 {
    display: none;
}

#hwork1.active {
    display: block;
}

Change your code like this - 像这样更改代码-

 function hide() { document.getElementById("fwork2").style.visibility = ""; document.getElementById("fwork3").style.visibility = ""; document.getElementById("fwork4").style.visibility = ""; document.getElementById("hwork1").style.visibility = ""; } function show() { document.getElementById("fwork1").style.visibility = "hidden"; document.getElementById("fwork2").style.visibility = "hidden"; document.getElementById("fwork3").style.visibility = "hidden"; document.getElementById("fwork4").style.visibility = "hidden"; document.getElementById("hwork1").style.visibility = "hidden"; } 

This is how you can change the visibility of an element with JavaScript. 这是您可以使用JavaScript更改元素的可见性的方法。 The current code changes the visibility property of some elements when the function is called / button is clicked. 当调用该函数/单击按钮时,当前代码更改某些元素的可见性属性。 It should be fairly straightforward how to apply this in another context. 如何在另一个上下文中应用它应该非常简单。

 function hideFourAndShowSix(){ let four = document.getElementById('four'); let six = document.getElementById('six'); six.style.setProperty('visibility', 'visible'); six.style.setProperty('display', 'list-item'); four.style.setProperty('visibility', 'hidden'); } 
 #six{ visibility: hidden; display: none; } 
 <ul> <li>One</li> <li>Two</li> <li>Three</li> <li id="four">Four</li> <li>Five</li> <li id="six">Six</li> </ul> <button onclick="hideFourAndShowSix()">Hide no. 4 and show no.6</button> 

PS: If you need extra explanation drop in a comment ;). PS:如果您需要其他说明,请在评论中添加;)。

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

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