简体   繁体   English

使div容器显示隐藏,然后使用按钮显示

[英]Getting div containers to display hidden and then show with a button

I am creating an admin panel and I have a panel on the left side of my page that I want to bring up different data. 我正在创建一个管理面板,并且页面左侧有一个面板,我想调出不同的数据。

I created a JSFiddle to show what I am doing. 我创建了一个JSFiddle以显示我在做什么。

The issue I am having is I want the dashboard home message... 我遇到的问题是我想要仪表板主页消息...

<div id="dashboard_home">Welcome to the Admin Dashboard</div>

To be the only div that shows up on page load. 成为页面加载时显示的唯一div。 Then when a panel seletion is clicked on, for the dashboard home message to go away and then only that new panel selection's div to show up. 然后,当单击面板选择时,仪表板主页消息消失,然后仅显示该新面板选择的div。

Then once another panel selection is clicked on, I want the previous selection to hide and the new one to display and so fourth for all of the selections. 然后,一旦单击另一个面板选择,我希望隐藏上一个选择,并显示新的选择,因此所有选择都排在第四位。

What do I need to do? 我需要做什么?

HTML HTML

<div class="panel_out">
    <div class="panel">
        <input type='button' class="panel_buttons" id='user_request_button' value='User Requests'>
        <input type='button' class="panel_buttons" id='message_button' value='Message Center'>
        <input type='button' class="panel_buttons" id='draft_order_button' value='Draft Order'>
        <input type='button' class="panel_buttons" id='draft_input_button' value='Draft Input'>
        <input type='button' class="panel_buttons" id='announcements_button' value='Announcements'>
        <input type='button' class="panel_buttons" id='dues_button' value='League Dues'>
    </div>
</div>
<div class="dashboard_selection">
    <div id='user_requests'>User Requests</div>
    <div id='message_center'>Message Center</div>
    <div id='draft_order'>Draft Order</div>
    <div id='draft_input'>Draft Input</div>
    <div id='announcements'>Announcements</div>
    <div id='dues'>Leauge Dues</div>
</div>

JS JS

jQuery(document).ready(function () {
    jQuery('#user_request_button').on('click', function (event) {
        jQuery('#user_requests').toggle('hide');
    });
    jQuery('#message_button').on('click', function (event) {
        jQuery('#message_center').toggle('hide');
    });
    jQuery('#draft_order_button').on('click', function (event) {
        jQuery('#draft_order').toggle('hide');
    });
    jQuery('#draft_input_button').on('click', function (event) {
        jQuery('#draft_input').toggle('show');
    });
    jQuery('#announcements_button').on('click', function (event) {
        jQuery('#announcements').toggle('show');
    });
    jQuery('#dues_button').on('click', function (event) {
        jQuery('#dues').toggle('show');
    });
});

Demo 演示

You're adding far too many bespoke events when you don't need to. 不需要时,您添加的定制事件太多了。 Normalise your IDs to match the button IDs and derive one from the other, 标准化您的ID以匹配按钮ID,并从另一个ID中派生一个,

eg <input id='user_requests_button' /> finds <div id="user_requests"> 例如, <input id='user_requests_button' />查找<div id="user_requests">

Show the div you want, then use siblings() to get the elements that you want hidden, and hide them. 显示所需的div,然后使用siblings()获取要隐藏的元素,然后将其隐藏。

Trigger the click event on the first button on load to show the first one only when the page loads (if you don't do this with CSS). 在加载时第一个按钮上触发click事件,以仅在页面加载时显示第一个按钮(如果您不使用CSS进行此操作)。

jQuery(document).ready(function () {
    $('.panel_out input').on('click', function(){
        // derive the ID
        var id_to_show = '#' + this.id.replace('_button', '');
        // show one and hide the others
        $(id_to_show).show().siblings().hide();
    }).first().trigger('click'); // trigger the first on page load
});

Trigger the click event on the first button on load to show the first one only when the page loads (if you don't do this with CSS). 在加载时第一个按钮上触发click事件,以仅在页面加载时显示第一个按钮(如果您不使用CSS进行此操作)。

On a click hide all panels first. 单击后,首先隐藏所有面板。 And then open the desired one with .show() 然后使用.show()打开所需的

So i would do it this way: 所以我会这样:

$('.panel').click(function(e){
 $('.panel').hide();
 $(e.currentTarget).closest('panel').show();
});

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

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