简体   繁体   English

使用没有功能的Javascript隐藏和显示div

[英]Hiding and showing divs with Javascript without multiple functions

How do I hide a child div of an LI without retyping a function for each set? 如何隐藏LI的子div而不为每个集合重新键入函数? I am planning to repeat the current html many times. 我打算将当前的html重复很多次。 Current Javascript: 当前的Javascript:

function $(id){
    return document.getElementById(id);
}
function year2014(){
    if ($('2014entries').style.display == 'none'){
        $('2014entries').style.display = 'block';
    }
    else {
        $('2014entries').style.display = 'none';
    }
}
function year2013(){
    if ($('2013entries').style.display == 'none'){
        $('2013entries').style.display = 'block';
    }
    else {
        $('2013entries').style.display = 'none';
    }
}

Partial HTML: 部分HTML:

<ul>
<li id="2014" onclick="year2014()">2014</li>
    <div id="2014entries">
        <li>content</li>
        <li>content</li>
        <li>content</li>
    </div>
<li id="2013" onclick="year2013()">2013</li>
    <div id="2013entries">
        <li>content</li>
        <li>content</li>
        <li>content</li>
    </div>
</ul>

Why you don't try something like: 为什么不尝试以下操作:

function year(yearValue){
    if ($(yearValue+'entries').style.display == 'none'){
        $(yearValue+'entries').style.display = 'block';
    }
    else {
        $(yearValue + 'entries').style.display = 'none';
    }
}

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

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