简体   繁体   English

如何在父div中隐藏/显示div

[英]How to hide/show a div inside a parent div

I am working on a site and I need to have a basic show hide functionality for part of it where, when the user clicks on a subject header, it displays an unordered list below the subject header, and then when they click on the subject header again, it will hide the content again. 我在一个网站上工作,我需要为网站的一部分提供基本的显示隐藏功能,其中,当用户单击主题标题时,它会在主题标题下方显示一个无序列表,然后当他们单击主题标题时再次,它将再次隐藏内容。

I have done this in the past, but when I tried applying my code to the site I am working on, it does not work....the hidden content does not display. 我过去曾经这样做过,但是当我尝试将代码应用到我正在工作的站点时,它不起作用....隐藏的内容不会显示。

Currently, I am using javascript, but if there is a better way, perhaps with pure CSS, that would be fine with me too. 当前,我正在使用javascript,但是如果有更好的方法(也许使用纯CSS),那对我也很好。

Here is the code I am trying right now (I have a feeling that it isn't working becasue of a parent/child div situation, but I wasn't sure and so I was hoping someone could lead me in the right direction: 这是我现在正在尝试的代码(由于父/子div的情况,我感觉它无法正常工作,但是我不确定,所以我希望有人可以将我引向正确的方向:

<div class="container">
<div class="row">
<div class="col-md-4">
<div class="toggle-buttons">
    <div class="web-develop-toggle">
        <a href="#" onClick="toggle-web-develop('web-develop-content',this)">
            <img src="images/subCtxHeader3-c.png" class="img-responsive" border="0">
        </a>
    </div>
    <div class="web-develop-content" style="display: none;">
        <div class="web-develop-body">
            <ul class="svc-list">
            <li class="svc-bullet"><span class="svc-list">Option 1</span></li>
            <li class="svc-bullet"><span class="svc-list">Option 2</span></li>
            <li class="svc-bullet"><span class="svc-list">Option 3</span></li>
            <li class="svc-bullet"><span class="svc-list">Option 4</span></li>
        </ul>
        </div>
    </div>
</div>
</div>
</div>
</div>

And here is the javascript that I am using currently, which has worked for me in the past, ironically enough: 这是我目前正在使用的javascript,在过去对我有用,具有讽刺意味的是:

<script type="text/javascript">

function toggle-web-develop(id, link) {

    var e = document.getElementById(id);

    if (e.style.display == '') {
        e.style.display = 'none';
    } else {
        e.style.display = '';
    }
}

</script>

Any and all help would greatly be welcomes as I have been stuck on this for a full day. 由于我一整天都坚持不懈,所以任何帮助都将受到欢迎。 Thanks in advance! 提前致谢!

-- Dan -丹

You cannot use - in a function name. 您不能在函数名称中使用- Try something like toggleWebDevelop() 尝试类似toggleWebDevelop()

You are trying to find web-develop-content by id, but you are using it as a class in your HTML 您正在尝试通过ID查找web-develop-content ,但是您将其用作HTML中的类

<div class="web-develop-content" style="display: none;">

Should be... 应该...

<div id="web-develop-content" style="display: none;">

Also, I would use e.style.display = 'block'; 另外,我将使用e.style.display = 'block'; instead of e.style.display = ''; 而不是e.style.display = '';

DEMO 演示

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

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