简体   繁体   English

单击链接时显示/隐藏div

[英]Showing/ hiding a div when click on a link

I am trying to show and hide a div when clicking a link. 我在点击链接时试图显示和隐藏div。 I got the div to show correctly, but I want that div to go away when I click a different one. 我得到了正确显示的div,但是当我点击另一个时,我希望该div消失。 This is the code I currently have. 这是我目前的代码。

<script type="text/javascript"> 
            $(function() {
                $('#attach_silver').click(function() {
                    $('#sec_silver').show();
                    return false;
                });        
            });

            $(function() {
                $('#attach_gold').click(function() {
                    $('#sec_gold').show();
                    return false;
                });        
            });

            $(function() {
                $('#attach_platinum').click(function() {
                    $('#sec_platinum').show();
                    return false;
                });        
            });
        </script>

<a href="#" id="attach_silver">Silver</a>
    <a href="#" id="attach_gold">Gold</a>
    <a href="#" id="attach_platinum">Platinum</a>

<div id="sec_silver" style="display: none;">
        Hello world!! Silver              
    </div>

    <div id="sec_gold" style="display: none;">
        Hello world!! Gold             
    </div>

    <div id="sec_platinum" style="display: none;">
        Hello world!! Platinum            
    </div>

try to add a class 尝试添加一个类

<div id="sec_silver" style="display: none;" class="divclass">
    Hello world!! Silver              
</div>

<div id="sec_gold" style="display: none;" class="divclass">
    Hello world!! Gold             
</div>

<div id="sec_platinum" style="display: none;" class="divclass">
    Hello world!! Platinum            
</div>

and after this code in jquery 并在jquery中的此代码之后

       $(function() {
                $('#attach_silver').click(function() {
                    $('.divclass').hide();
                    $('#sec_silver').show();
                    return false;
                });        
            });

            $(function() {
                $('#attach_gold').click(function() {
                    $('.divclass').hide();
                    $('#sec_gold').show();
                    return false;
                });        
            });

            $(function() {
                $('#attach_platinum').click(function() {
                    $('.divclass').hide();
                    $('#sec_platinum').show();
                    return false;
                });        
            });

In this mode you hide all div on a click event, and show the div that you want 在此模式下,您将隐藏单击事件上的所有div,并显示所需的div

您的点击事件当前显示相关的div,您只需要它们也可以隐藏其他div。

Use ^ attribute-starts with selector 使用^ attribute-starts和selector

$('div[id^="sec_"]').hide(); // will hide all the div with id starting with sec_

your code becomes 你的代码变成了

$(function () {
    $('#attach_silver').click(function () {
        $('div[id^="sec_"]').hide();// add here
        $('#sec_silver').show();
        return false;
    });
    $('#attach_gold').click(function () {
        $('div[id^="sec_"]').hide();// add here
        $('#sec_gold').show();
        return false;
    });
    $('#attach_platinum').click(function () {
        $('div[id^="sec_"]').hide();// add here
        $('#sec_platinum').show();
        return false;
    });
});

You only show the other divs, you never tell them to hide: 你只显示其他div,你永远不会告诉他们隐藏:

<script type="text/javascript"> 
$(function () {
    $('#attach_silver').click(function () {
        console.log('sliver click');
        $('#sec_silver').show();
        $('#sec_gold').hide();
        $('#sec_platinum').hide();
        return false;
    });
});

$(function () {
    $('#attach_gold').click(function () {
        $('#sec_gold').show();
        $('#sec_silver').hide();
        $('#sec_platinum').hide();
        return false;
    });
});

$(function () {
    $('#attach_platinum').click(function () {
        $('#sec_platinum').show();
        $('#sec_gold').hide();
        $('#sec_silver').hide();
        return false;
    });
});
</script>

How about a two line solution 两线解决方案怎么样?

$('a').click(function () {
    $('div:contains('+$(this).text()+')').show();
    $('div:not(:contains('+$(this).text()+'))').hide();
});

Live Demo 现场演示

I like the id/class pattern. 我喜欢id / class模式。 Also, by adding a class you can remove that inline style. 此外,通过添加类,您可以删除该内联样式。 Add: 加:

.divclass{
    display: none
}

To do it with javascript: 要用javascript做到这一点:

<a href="#xyz" onclick="hideDiv();">Hide Div</a>

function hideDiv()
{
 document.getElementById('myContainerDiv').style.display = "none";
}

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

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