隐藏除所选列表选项之外的列表

发布于 2024-10-31 13:51:34 字数 441 浏览 4 评论 0原文

我需要一些有关嵌套列表的帮助,我想要做的是显示列表选项之一并隐藏列表的其余部分,我看到的一个很好的例子是在 filesonic.com 语言选择器或 netlog 状态更改中标头。

<ul class="links">
  <li class="us">United States</li>
  <li class="canada">Canada</li>
  <li class="france">France</li>
  <li class="china">China</li>
  <li class="englande">England</li>

美国是默认设置,但当有人单击法国时,列表的其余部分将隐藏并显示。

提前致谢。

I need some help with a nested list, what I want to do is to show one of the list options and hide the rest of the list, a great example that I saw was in the filesonic.com languages selector or netlog status change in the header.

<ul class="links">
  <li class="us">United States</li>
  <li class="canada">Canada</li>
  <li class="france">France</li>
  <li class="china">China</li>
  <li class="englande">England</li>

United States is the default but when someone click on France, the rest of the list will hide and show will show.

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

划一舟意中人 2024-11-07 13:51:34
$(function(){
  $('ul.links > li').click(function(){
      $('ul.links > li').fadeOut();
      $(this).fadeIn();
  });
});
$(function(){
  $('ul.links > li').click(function(){
      $('ul.links > li').fadeOut();
      $(this).fadeIn();
  });
});
巷子口的你 2024-11-07 13:51:34

像这样的东西就可以解决问题:

// Initially hide everything apart from the active list item
$('ul li.active').show()
  .siblings().hide();

// Set up a click handler that will show all the list items when the active
// element is clicked on, or makes the clicked on item the active item and hides
// the rest.
$('ul li').click(function(){
  var $parent=$(this).parent();
  if ($parent.is('.open')){
    $parent.removeClass('open');
    $(this).addClass('active')
      .siblings().removeClass('active').hide();
  }
  else {
    $parent.addClass('open').children().show();
  }
});

这是一个有效的 JSBIN 示例: http://jsbin.com/onile4

希望这有帮助!

Something like this will do the trick:

// Initially hide everything apart from the active list item
$('ul li.active').show()
  .siblings().hide();

// Set up a click handler that will show all the list items when the active
// element is clicked on, or makes the clicked on item the active item and hides
// the rest.
$('ul li').click(function(){
  var $parent=$(this).parent();
  if ($parent.is('.open')){
    $parent.removeClass('open');
    $(this).addClass('active')
      .siblings().removeClass('active').hide();
  }
  else {
    $parent.addClass('open').children().show();
  }
});

Here is a working JSBIN example: http://jsbin.com/onile4

Hope this helps!

平生欢 2024-11-07 13:51:34

如果选择“我们”,您可以:

$('ul > li[class!=us]').css('display', 'none');

If 'us' was selected, you could:

$('ul > li[class!=us]').css('display', 'none');
夜还是长夜 2024-11-07 13:51:34

你可以使用类似的东西:

if ($('.selected').length){
    return false;
}
else {
    $('.links li:first').addClass('selected');
}
$('.links li').click(
    function(){
        $('.selected').removeClass('selected');
        $(this).addClass('selected');
    });

加上CSS:

li {
    display: none;
}

li.selected {
    display: list-item;
}

ul:hover li {
    display: list-item;
}

JS Fiddle demo

You could use something like:

if ($('.selected').length){
    return false;
}
else {
    $('.links li:first').addClass('selected');
}
$('.links li').click(
    function(){
        $('.selected').removeClass('selected');
        $(this).addClass('selected');
    });

coupled with the CSS:

li {
    display: none;
}

li.selected {
    display: list-item;
}

ul:hover li {
    display: list-item;
}

JS Fiddle demo.

标点 2024-11-07 13:51:34

带有注释的快速脏样本(您可以根据您的需要即兴创作):

工作示例@ http://jsfiddle.net /dCZpx/

<div style="width: 100px">
    <div style="background-color: #FFDDEE" id="country">Click here to Select Country</div>
    <ul class="links">   
        <li class="us">United States</li>   
        <li class="canada">Canada</li>   
        <li class="france">France</li>   
        <li class="china">China</li>   
        <li class="englande">England</li> 
    </ul>
    <script type="text/javascript">    
        $(function(){

            //Bind the Click handler for the box that displays the selected Country
            $("#country").click(function(){
                //When the Country Box is clicked, show all the country names except the selected one
                $(".links li").not("." + $(this).data("current")).show("slow"); 
            });

            //First Hide all the Country Names list 
            $(".links li").hide();

            //Bind OnClick handler to all list elements which are children of .links
            $(".links li").click(function(){
                //Set the currently selected country in the data attribute of the #country element.
                $("#country").data("current", $(this).attr("class"));
                //Set the text of the #country div to the selected country name
                $("#country").text($(this).text());
                //Now hide all the list elements 
                $(".links li").hide("slow");
            });
               $(".links li.us").click();
        });
    </script> 
</div>

A quick n dirty sample with comments(you can improvize it based on your needs):

Working example @ http://jsfiddle.net/dCZpx/

<div style="width: 100px">
    <div style="background-color: #FFDDEE" id="country">Click here to Select Country</div>
    <ul class="links">   
        <li class="us">United States</li>   
        <li class="canada">Canada</li>   
        <li class="france">France</li>   
        <li class="china">China</li>   
        <li class="englande">England</li> 
    </ul>
    <script type="text/javascript">    
        $(function(){

            //Bind the Click handler for the box that displays the selected Country
            $("#country").click(function(){
                //When the Country Box is clicked, show all the country names except the selected one
                $(".links li").not("." + $(this).data("current")).show("slow"); 
            });

            //First Hide all the Country Names list 
            $(".links li").hide();

            //Bind OnClick handler to all list elements which are children of .links
            $(".links li").click(function(){
                //Set the currently selected country in the data attribute of the #country element.
                $("#country").data("current", $(this).attr("class"));
                //Set the text of the #country div to the selected country name
                $("#country").text($(this).text());
                //Now hide all the list elements 
                $(".links li").hide("slow");
            });
               $(".links li.us").click();
        });
    </script> 
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文