简体   繁体   English

使用jQuery在页面上切换DIV

[英]Toggle DIVs on a page using jQuery

I have a row of links across the top of my page which are meant to act as tabs, when one is clicked I want its DIV to show and the rest to hide. 我的页面顶部有一排链接,用作标签,当点击一个时,我希望它的DIV显示,其余的隐藏。 I'm really not sure the best way to go about this. 我真的不确定最好的方法。

What I originaly came up with: 我最初想出的是:

$("#overall").click(function(){
    if ($("#overall").is(":hidden"))
    {
        $("#hourly-div").hide("fast");
        $("#daily-div").hide("fast");
        $("#monthly-div").hide("fast");
        $("#overall-div").show("fast");
    }
});

But I found out this doesn't work well at all. 但我发现这根本不起作用。 Should I be checking if the CSS for the DIV is display: hidden; 我应该检查是否display: hidden; DIV的CSS display: hidden; ?

Mark up below. 在下面标记。

    <ul class="stats">
        <li><a href="#" id="overall">Overall stats</a></li>
        <li class="sep">|</li>
        <li><a href="#" id="hourly">Hourly Bandwidth</a></li>
        <li class="sep">|</li>
        <li><a href="#" id="daily">Daily Bandwidth</a></li>
        <li class="sep">|</li>
        <li><a href="#" id="monthly">Monthly Bandwidth</a></li>
    </ul>
    <div id="overall-div">
        overall
    </div>
    <div id="hourly-div" style="display: none;">
        hourly
    </div>
    <div id="daily-div" style="display: none;">
        daily
    </div>
    <div id="monthly-div" style="display: none;">
        monthly
    </div>

I wanted the overall div to show when a user loads the page, and the rest to be hidden until a user clicks a tab for them. 我希望overall div在用户加载页面时显示,其余部分在用户单击其选项卡之前隐藏。

Thanks. 谢谢。 :) :)

The best way to go about it is to use jQuery UI and the tabs plugin. 最好的方法是使用jQuery UItabs插件。 If you must implement a tabbed interface yourself, at least look at their code for idea on how to go about it. 如果您必须自己实现选项卡式界面,至少要查看他们的代码,了解如何进行操作。 Note that you don't have to download the entire UI code to use just the tabs. 请注意,您不必下载整个UI代码以仅使用选项卡。 You can make the download smaller by restricting it to just those components that you need. 您可以通过将下载限制为所需的组件来减小下载量。

<div class="tabs">
    <ul class="stats">
            <li><a href="#overall-div" id="overall">Overall stats</a></li>
            <li><a href="#hourly-div" id="hourly">Hourly Bandwidth</a></li>
            <li><a href="#daily-div" id="daily">Daily Bandwidth</a></li>
            <li><a href="#monthly-div" id="monthly">Monthly Bandwidth</a></li>
    </ul>
    <div id="overall-div">
            overall
    </div>
    <div id="hourly-div">
            hourly
    </div>
    <div id="daily-div">
            daily
    </div>
    <div id="monthly-div">
            monthly
    </div>
</div>

<script type="text/javascript">
    $(function() {
        $('.tabs').tabs();  // that's it unless you want to customize
    });
</script>

A nice added bonus to this is that it works just fine if the user has javascript disabled. 一个很好的额外奖励是,如果用户禁用了JavaScript,它可以正常工作。

Something like: 就像是:

$('ul.stats a').click(function(e) {
    $('#' + this.id + '-div').show().siblings('[id$=-div]').hide();
    e.preventDefault();
});

Here's how I've done it, add this to a click event: 以下是我的完成方式,将其添加到点击事件中:

if(jQuery){
  jQuery('.tab').removeClass('show');
  jQuery('#skills').addClass('show');
  return false;
};

and css: 和css:

div.tab {display:none;}
div.show {display:block;}

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

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