简体   繁体   English

切换div的类以根据单击的按钮显示/隐藏内容

[英]Toggling the class of a div to show/hide content based on which button is clicked

I am trying to show/hide a list of menu items based on which button is clicked for that particular category. 我试图根据针对该特定类别单击的按钮来显示/隐藏菜单项列表。 Most Popular is seen by default/on page load. 默认情况下/页面加载时会看到最热门。 So for example when I click on the Appetizers button the page should go from this to this . 因此,例如,当我点击按钮,开胃的页面应该走这个

I am still very new to jQuery and JavaScript so I am having some difficulties getting this to work. 我仍然是jQuery和JavaScript的新手,所以我在使用它时遇到了一些困难。 Here is the HTML (condensed, just wanted to show what was seen in the screenshots and not all of the other menu items): 这是HTML(浓缩,只是想显示屏幕截图中显示的内容而不是所有其他菜单项):

<div class="menu-container">
    <div class="menu-preview">
        <div class="menu-preview-items active">
            <h2>Most Popular</h2>
            <div class="menu-listing">
                <h3>California Roll (8 Pieces)</h3>
                <p>Imi crab, cucumber, avocado, and mayo with sesame</p>
                <p>$3.50</p>
            </div>
            <div class="menu-listing">
                <h3>Dynamite Roll (8 Pieces)</h3>
                <p>Two pieces prawn tempura, yam, cucumber, avocado, masago, letters, and mayo with sesame</p>
                <p>$4.95</p>
            </div>
        </div>
        <div class="menu-preview-items hidden">
            <h2>Appetizers</h2>
            <div class="menu-listing">
                <h3>Edamame</h3>
                <p>Boiled green soy bean with salt.</p>
                <p>$3.50</p>
            </div>
            <div class="menu-listing">
                <h3>Gomae</h3>
                <p>Blanched spinach with sesame seed and peanut sauce.</p>
                <p>$3.50</p>
            </div>
        </div>
    </div>
    <div class="menu-categories">
        <a href="#" class="menu-box">Most Popular</a>
        <a href="#" class="menu-box">Appetizers</a>
        <a href="#" class="menu-box">A La Carte</a>
        <a href="#" class="menu-box">BBQ</a>
        <a href="#" class="menu-box">Salad & Soup</a>
        <a href="#" class="menu-box">Tempura</a>
    </div>
</div>

Here is the CSS for the classes of .active and .hidden (as well as the container): 下面是的类CSS .active.hidden (以及容器):

.active {
    display: none;
}

.hidden {
    display: contents;
}

.menu-container {
    margin-top: 15px;
    display: flex;
    justify-content: space-evenly;
}

Here is the script that I have so far as well (which is placed at the very bottom of the document, just above the closing body tag): 这是我到目前为止的脚本(它位于文档的最底部,就在关闭正文标记的上方):

$('.menu-box').click(function () {
    $('.menu-preview-items').toggleClass('.active');
    // alert('Hello!');
});

I will of course be adding more menu items for all the categories over time, I just wanted to try and get this to work before I commit to doing so. 我当然会随着时间的推移为所有类别添加更多菜单项,我只是想在我承诺之前尝试让它工作。 Should I be adding id's to anything? 我应该在任何地方添加id吗? Do I need to add/edit any of my class names? 我是否需要添加/编辑任何类名? Any help would be greatly appreciated! 任何帮助将不胜感激!

You can try this: 你可以试试这个:

change css 改变css

    .active {
display: block;
}

.hidden {
display: none;
}

.menu-container {
margin-top: 15px;
display: flex;
justify-content: space-evenly;
}

The html part html部分

<div class="menu-container">

            <div class="menu-preview">
                <div class="menu-preview-items active most-popular">

                    <h2>Most Popular</h2>

                    <div class="menu-listing">
                        <h3>California Roll (8 Pieces)</h3>
                        <p>Imi crab, cucumber, avocado, and mayo with
                            sesame</p>
                        <p>$3.50</p>
                    </div>

                    <div class="menu-listing">
                        <h3>Dynamite Roll (8 Pieces)</h3>
                        <p>Two pieces prawn tempura, yam, cucumber,
                            avocado, masago, letters, and mayo with sesame</p>
                        <p>$4.95</p>
                    </div>

                </div>

                <div class="menu-preview-items hidden appetizers">

                    <h2>Appetizers</h2>

                    <div class="menu-listing">
                        <h3>Edamame</h3>
                        <p>Boiled green soy bean with salt.</p>
                        <p>$3.50</p>
                    </div>

                    <div class="menu-listing">
                        <h3>Gomae</h3>
                        <p>Blanched spinach with sesame seed and peanut sauce.</p>
                        <p>$3.50</p>
                    </div>

                </div>
            </div>

            <div class="menu-categories">
                <a href="#" class="menu-box" ref="most-popular">Most Popular</a>

                <a href="#" class="menu-box" ref="appetizers">Appetizers</a>

                <a href="#" class="menu-box">A La Carte</a>

                <a href="#" class="menu-box">BBQ</a>

                <a href="#" class="menu-box">Salad & Soup</a>

                <a href="#" class="menu-box">Tempura</a>

            </div>

        </div>

Now the JQuery 现在是JQuery

$('.menu-categories .menu-box').on('click',function(){
      $('.menu-preview-items').addClass('hidden');
      $('.menu-preview-items').removeClass('active');
      $('.'+$(this).attr('ref')).removeClass('hidden');
      $('.'+$(this).attr('ref')).addClass('active');
    });

You can have this done with JavaScript. 您可以使用JavaScript完成此操作。 Set the elements you want to show and hide by giving each and id. 通过给出每个和id来设置要显示和隐藏的元素。 In your JavaScript file, set those elements to what you want when the event to trigger this is clicked. 在JavaScript文件中,将单击这些元素设置为单击要触发此事件的事件。 There are different ways to go about this. 有不同的方法来解决这个问题。 You can use pure JavaScript by doing: document. 您可以通过执行以下操作来使用纯JavaScript:document。 getElementById('name of id').remove() or use the CSS, document. getElementById('name of id')。remove()或使用CSS,document。 getElementById('name of id').style.display = 'none' or 'block' based on displaying the elements or hiding the elements. getElementById('id of name')。style.display ='none'或'block'基于显示元素或隐藏元素。

If you have further questions, do not hesitate to ask 如果您还有其他问题,请随时提出

You are missing some way of linking the menu container click to the menu contents. 您缺少一些链接菜单容器单击菜单内容的方法。

You will need to add ids to every div that has the menu-listing class. 您需要将id添加到具有menu-listing类的每个div。

Each of your menu-box links need to somehow know which menu-listing to activate. 每个menu-box链接都需要以某种方式知道要激活的menu-listing You also need to de-activate all of the other menu-listing s. 您还需要取消激活所有其他menu-listing

I'd recommend using data-* attributes . 我建议使用data- *属性 These allow you to add attributes to any DOM element. 这些允许您向任何DOM元素添加属性。 In this case, you'd add a data attribute to your menu-box links that reference the menu-listing you care about. 在这种情况下,您需要在menu-box链接中添加一个数据属性,以引用您关注的menu-listing It would look something like this: 它看起来像这样:

The menu listing: 菜单列表:

<div id="most-popular" class="menu-preview">
  ...
</div>

The menu box: 菜单框:

<a href="#" class="menu-box" data-menu-listing="most-popular">Most Popular</a>

And finally, the jquery: 最后,jquery:

$('.menu-box').click(e => {
  let id = e.target.data('menu-listing');
  // first remove the class from all items
  $('.menu-preview-items').removeClass('.active');
  // now add back the active class for the item we care about
  $(`#${id}`).addClass('.active');
});

You could user the .hide() and .show() functions in jQuery if you would prefer: 如果您愿意,可以在jQuery中使用.show() .hide().show()函数:

 $(document).ready(function () { $("#item2").hide(); $("#item3").hide(); $("#item4").hide(); }); function showItem(id) { // Hide all of the items $('.items').hide(); itemId = "item" + id; $("#" + itemId).show(); }; 
 .container { display: flex; flex-direction: row; justify-content: space-between; } .buttons-container { display: flex; flex-direction: row; justify-content: space-between; width: 300px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="items-container"> <p id="item1" class="items">Item1</p> <p id="item2" class="items">Item2</p> <p id="item3" class="items">Item3</p> <p id="item4" class="items">Item4</p> </div> <div class="buttons-container"> <a id="btn1" href="#" onclick="showItem(1)">Btn1</a> <a id="btn2" href="#" onclick="showItem(2)">Btn2</a> <a id="btn3" href="#" onclick="showItem(3)">Btn3</a> <a id="btn4" href="#" onclick="showItem(4)">Btn4</a> </div> </div> 

What this code does it hides all but the first item on document.ready (You could just set these to your hidden class). 这段代码除了document.ready上的第一项外,它隐藏了所有内容(您可以将它们设置为隐藏类)。 Then when you click on an item it hides all items with the class items and then shows the item corresponding with the button you clicked. 然后,当您单击某个项目时,它会隐藏包含类别items所有项目,然后显示与您单击的按钮对应的项目。

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

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