简体   繁体   English

显示/隐藏或切换同一类的多个元素,一次单击仅显示一个

[英]Show/Hide OR Toggle multiple elements with same class, only show one at a time on click

Alright. 好的。 So I'm going to try and explain this the best I can. 因此,我将尽力解释这一点。 I just don't understand how to use jQuery in HTML and a javascript answer would be most appreciated, but I'll take jQuery if you can give me HTML with it, and not just jQuery code. 我只是不明白如何在HTML中使用jQuery,而对JavaScript的回答将是最好的选择,但是如果您可以给我HTML,而不仅仅是jQuery代码,我将使用jQuery。

I have a guide page, the guide page itself is embedded into the main page using php (index.php?content=guide). 我有一个指南页面,该指南页面本身是使用php(index.php?content = guide)嵌入到主页中的。 On the guide page, I would like to use a show/hide OR a toggle function of sorts to toggle certain parts of the page, as related to the guide. 在指南页面上,我想使用显示/隐藏或各种切换功能来切换页面的某些部分(与指南相关)。 Then, I would like to only show one at a time, as one link is clicked, only show the clicked elements and hide the rest. 然后,我想一次只显示一个,因为单击了一个链接,所以只显示被单击的元素并隐藏其余元素。 The list is on the left hand side of the page, while the content to be shown is on the right. 列表在页面的左侧,而要显示的内容在右侧。

`http://jsfiddle.net/wesman2232/2m2jjcc9/`

(Why isn't toggle working on jsfiddle? It's working fine on my local webserver) (为什么不切换在jsfiddle上工作?在我的本地网络服务器上工作正常)

Normally I would just make a different php page for each and every subsection (index.php?content=guide&do=requirements) but I want to try and get away from that, so I'm trying to just get it in one whole page 通常,我只会为每个小节制作一个不同的php页面(index.php?content = guide&do = requirements),但我想尝试摆脱这一点,所以我试图将其放在一个整页中

Currently, it toggles like I want, but I would rather have it all in one script instead of having to write out something like show('news'); 目前,它可以按我想要的方式进行切换,但是我宁愿将它们全部放在一个脚本中,而不必写出诸如show('news')之类的东西; show('news2'); show('news2'); hide('updates'); hide('updates'); hide('updates2'); hide('updates2'); etc etc. Also if you just kept clicking on Synopsis it would toggle between the two instead of just switching to that one, and that one only. 同样,如果您只是一直单击“概要”,它将在两者之间切换,而不仅仅是切换到那个和那个。

I get that this isn't multiple, but for the multiple part is where I have something like this on the media page: 我知道这不是多重的,但是对于多重而言,我在媒体页面上有这样的内容:

`http://jsfiddle.net/wesman2232/mwnua9q8/`

except I would change it from using id to class and set them all having the same class right? 除非我会将其从使用id更改为类,并将它们都设置为具有相同的类?

Guess I need to go back and re-learn everything cause I'm having a hard time wrapping my brain around all this haha. 猜猜我需要回去重新学习一切,因为我很难把自己的大脑包裹在所有这些哈哈上。 I've tried reading other questions but still can't get it to work the way I want it to. 我尝试阅读其他问题,但仍然无法按照我想要的方式工作。

The problem with your code, in my opinion, is that it is outdated. 我认为您的代码存在问题,因为它已经过时了。 You are using tables for non tabular design and you are making it difficult for yourself using pure JS in comparison to jQuery. 您正在使用tables进行非表格设计,与jQuery相比,您很难使用纯JS。

You need to do the following: 您需要执行以下操作:

  • do away with tables and use divs divs tables并使用divs
  • use the powerful ways of styling your content with CSS3 使用CSS3样式化内容的强大方法
  • even though it is very good to know plain JS, use jQuery to make coding fun and easy. 即使知道普通的JS很好,也可以使用jQuery使编码变得有趣而容易。
  • always try to make your HTML as clean as possible with the use of IDs and Classes 始终尝试使用ID和类使HTML尽可能整洁

This is the way I would redo your code: 这是我重做代码的方式:

 $(".link").click(function (e) { e.preventDefault(); $(".content").removeClass("active"); var content_id = $(this).attr("href"); $(content_id).addClass("active"); }); 
 #wrapper { text-align: center; } #content_container { margin-top: 10px; } .content:not(.active) { display: none; } .title{ font-weight:bold; text-transform: capitalize; font-size: 30px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <div id="wrapper"> <div> <a class="link" href="#screenshots">Screenshots</a> | <a class="link" href="#videos">Videos</a> | <a class="link" href="#wallpaper">Wallpapers</a> </div> <div id="content_container"> <div id="screenshots" class="content active"> <div class="title">Screenshots</div> <div class="description">Screenshot description</div> </div> <div id="videos" class="content"> <div class="title">Videos</div> <div class="description">Videos description</div> </div> <div id="wallpaper" class="content"> <div class="title">Wallpapers</div> <div class="description">Wallpapers description</div> </div> </div> </div> 

Try this. 尝试这个。 It's a jquery version. 这是一个jQuery版本。

    $().ready(function()
    {
   $('#gm, #pg').click(function()
{
    if($(this).attr("id") == "gm")
    {
        $("#gameinformation").toggle();
        if($("#playguide").is(":visible"))
        $("#playguide").toggle();
    }
    else
    {
        $("#playguide").toggle();
        if($("#gameinformation").is(":visible"))
        $("#gameinformation").toggle();
    }
});
    });

Fiddle : http://jsfiddle.net/2m2jjcc9/2/ 小提琴: http : //jsfiddle.net/2m2jjcc9/2/

ALTERNATE APPROACH : 替代方法:

If you multiple section to toggle, you may use the following. 如果要切换多个部分,则可以使用以下内容。 I used an approach to tag each corresponding 'UL' tags of 'a' with an attribute called 'data-dependency' and use it to toggle sections. 我使用了一种方法,使用名为“数据依赖”的属性来标记“ a”的每个对应“ UL”标记,并使用它来切换部分。

$('a').click(function()
{
    var $this = $(this).attr('id');
    $('a').each(function()
    {
        var id = $(this).attr('id');
        if(id == $this)
        {           
             $("ul[data-dependency='" + id + "']").toggle();           
        }
        else            
        {
           if($("ul[data-dependency='" + id + "']").is(":visible"))
           $("ul[data-dependency='" + id + "']").toggle();
        }
    });
});

Fiddle : http://jsfiddle.net/2m2jjcc9/4/ 小提琴: http : //jsfiddle.net/2m2jjcc9/4/

UPDATED BASED ON REQUIREMENT TO KEEP ALL MENUS OPTIONS OPEN : 根据需要保留所有菜单选项的更新:

$('a').click(function()
{    
    var $this = $(this).attr('id');
    if($("ul[data-dependency='" + $this + "']")[0] != undefined)
      $("ul[data-dependency='" + $this + "']").toggle();
    else
        $(this).closest('li').find('ul').toggle();
});

Fiddle : http://jsfiddle.net/2m2jjcc9/5/ 小提琴: http : //jsfiddle.net/2m2jjcc9/5/

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

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