简体   繁体   English

jQuery / Javascript在li中隐藏/显示div

[英]Jquery/Javascript to hide/show div in li

I have many div's on a page, like the one below with different information, using the same classes. 我在页面上有很多div,例如以下使用相同类的具有不同信息的div。

<div class="one-third">
    <ul class="img-list">
        <a href="#" target="_blank">
            <li>
                <img src="#" alt="" />
                <span class="text-content"><h3 style="color:white" id="one">BRIAN FARGO</h3></span>
                <h5 class="text-content-desc" id="two">CEO, inXile entertainment</h5>
                <a href="https://twitter.com/BrianFargo">
                <img class="speaker-twitter-profile" src="#"></a>
                <div class="info-btn" onClick="swap('one','two')">info</div>
            </li>
        </a>
    </ul>
</div>

And a javascript functions is: 而javascript函数是:

function swap(one, two) {
    document.getElementById(one).style.display = 'block';
    document.getElementById(two).style.display = 'none';
}

The problem is, when I click on the "info" button, it effects on all div's, but I need it to only effect on the div which button is clicked. 问题是,当我单击“信息”按钮时,它会影响所有div,但是我只需要它对单击了该按钮的div起作用。

Like if I click on info of BRIAN FARGO, it should only show/hide/effect BRIAN FARGO'S bio. 就像我单击BRIAN FARGO的信息一样,它只应显示/隐藏/影响BRIAN FARGO的履历。 Not all other speakers. 并非所有其他发言人。

I don't understand why is your function named swap() when all it does is changes the display attribute of one element to block (which it already is by default) and changes another elements display to none effectively hiding it. 我不明白为什么当您的函数将全部内容更改为block(默认情况下已经为默认值)并将另一个元素的显示属性更改为none(有效)时,将其命名为swap()的原因。

If you have many divs with the same id, that too could be your problem. 如果您有许多具有相同ID的div,那也可能是您的问题。 Id should be unique for each element 每个元素的ID应该唯一

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

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