我试图获得一个转换的效果,但不是很确定如何做到这一点,或者如何寻找。(i灵真的太好了Jquery载).

假设html类似于

<table class="left-dates">
    <tr><td>All Dates</td></tr>
    <tr><td>01 dec 2009</td></tr>
    <tr><td>02 dec 2009</td></tr>   
    <tr><td>03 dec 2009</td></tr>   
    <tr><td>04 dec 2009</td></tr>   
</table>

<div class="box 01-dec-2009">
    foo
</div>

<div class="box 03-dec 2009">
    bar
</div>

<div class="box 04-dec-2009">
    foobar
</div>

要采取的步骤

  1. 所有div的示
  2. 点击td一个日期将隐藏所有,但div类的天点击
  3. 点击"所有日期"将显示一切了

任何想法如何能实现这个干净?理想情况下使用的!

有帮助吗?

解决方案

这里是工作的例子!

注意,我不得不改变你的div类和 td 标签,以消除的空白,以便标签将相当于类名称。如果你不想破折号的标签,你能做串操纵Javascript中删除的空白或得到 tds相同类作为其相应的div然后看看类的点击 td 而不是它的内的案文。

<html>
<head>
    <title>jQuery hiding example</title>

    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>

    <script type='text/javascript'>
        $(document).ready(function(){
            $('td').click(function() {
                var target = $(this).text();
                if (target == 'All Dates') {
                    $('div.box').show();
                } else {
                    $('div.box').hide();
                    $('div.' + target).show();
                }
            });
        });
    </script>
</head>
<body>
    <table class="left-dates">
        <tr><td>All Dates</td></tr>
        <tr><td>01-dec-2009</td></tr>
        <tr><td>02-dec-2009</td></tr>       
        <tr><td>03-dec-2009</td></tr>       
        <tr><td>04-dec-2009</td></tr>       
    </table>

    <div class="box 01-dec-2009">
        foo
    </div>

    <div class="box 03-dec-2009">
        bar
    </div>

    <div class="box 04-dec-2009">
        foobar
    </div>
</body>
</html>

其他提示

我会尝试这种方式:

var $boxes = $('div.box');

$('.left-dates td:gt(0)').click(function(e){
   var class = this.innerHTML.replace(/ /g, '-'); // Convert text to class
   $boxes.filter(':visible').not('.' + class).hide(); // All visible div.box that don't have the class we are going to show.
   $boxes.filter('.' + class).show(); // Show this class
});
$('.left-dates td:first').click(function(e){
   $boxes.show();
});

编辑: 我交换 .click() 在为 .live('click').如果有人要进一吨的排,这可能是更好的使用 .live() 而不是结合一个 click() 事件向每一个 td

此外,该HTML你发布了一个错误。的 03 div是缺少一个连字之前 2009

识别你 <td>All Dates</td> 独特。指定同一类或一些其他标识到你所有的日期 <td>s.给他们点击的处理程序隐藏了所有的.框元素,除了一个与同一日期。在你的例子你是不符合制作的日期在 <td> 相同类名称的日期在你div,你会需要我会做什么。

<table class="left-dates">
    <tr><td id="alldates">All Dates</td></tr>
    <tr><td id="date">01 dec 2009</td></tr>
    <tr><td id="date">02 dec 2009</td></tr>       
    <tr><td id="date">03 dec 2009</td></tr>       
    <tr><td id="date">04 dec 2009</td></tr>       
</table>

// unhide all box elements
$("#alldates").click(function(e){ $(".box").show(); });

// For each date <td> element
$("#date").each(function(){
   // assign a click event
   $(this).click(function(e){
      // when the user clicks, get the
      // <td>'s text contents
      var myval = $(this).text();
      // and grab each element with the
      // 'box' css class
      $(".box").each(function(){
         // for each of these 'box' elements,
         // if it has a class matching the date
         // they selected, return
         if($(this).has(myval))
         {
            return;
         }
         else
         {
            // otherwise, hide itself
            $(this).hide();
         }
      });
   });
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top