【问题标题】:jquery hide() not working after page loads页面加载后jquery hide()不起作用
【发布时间】:2013-07-16 14:40:21
【问题描述】:

我想我在这里遗漏了一些简单而明显的东西 - 我正在一个需要过滤投资组合类别的网站上工作,并在链接点击时显示类别描述,但不适用于“全部”列表项.

页面加载正常,然后我可以单击并过滤类别/描述,但如果您单击返回“全部”,则最后单击的类别描述仍然存在,即使它应该隐藏。

这里是开发网站 (Wordpress) - http://foothilltile.com/dev/products/

下面是相关代码:

<ul class="filter-nav">
  <li>
    <h4><?php _e("Filter:", "elemis"); ?></h4>
  </li>
  <li class="selected-1 all"><a href="#" data-value="all">
    <h4><?php _e("All", "elemis"); ?></h4>
    </a></li>
    <?php 
      $categories=  get_categories('taxonomy=kind&orderby=id'); 
      foreach ($categories as $cat) {
        $input = '<li><a href="#" data-value="'.$cat->category_nicename.'" class="'.$cat->category_nicename.'"><h4>';
        $input .= $cat->cat_name;
        $input .= '</h4></a></li>';
        echo $input;
      }
     ?>
</ul>
<div class="clear"></div>
<!-- End Portfolio Navigation --> 

<div id="category-descriptions">

    <ul class="descriptions-list">
      <li class="all"></li>
      <?php 
          $categories=  get_categories('taxonomy=kind&orderby=id'); 
          foreach ($categories as $cat) {
          $input = '<li class="cat-desc '.$cat->category_nicename.'">';
          $input .= $cat->description;
          $input .= '</li>';
          echo $input;
          }
      ?>

    </ul>
</div> <!--/category-descriptions -->`

然后是js:

jQuery(document).ready(function($) {
  $('.filter-nav li a').click(function() {
    // fetch the class of the clicked item
    var ourClass = $(this).attr('class');

    if (ourClass == 'all') {
      // this should hide the li's on load - but doesn't? 
      $('.descriptions-list').children('li.cat-desc').hide();
    }
    else {

      // hide all elements that don't share ourClass
      $('.descriptions-list').children('li:not(.' + ourClass + ')').hide();
      // show all elements that do share ourClass
      $('.descriptions-list').children('li.' + ourClass).show();
  }
    return false;
  });
});

唯一相关的 css 是 li.cat-desc 设置为 display:none。

任何建议将不胜感激。我总是混淆 jQuery 中的代码顺序(当然是菜鸟),所以提前感谢您的帮助。

【问题讨论】:

    标签: jquery wordpress filtering


    【解决方案1】:

    “All”的锚点没有“all”类……这就是问题所在。类设置为 锂元素... 将类添加到 a 元素,它应该可以工作

    【讨论】:

    • 非常感谢 s4ty - 这正是我所看到的 - 我的眼睛一直在想念它。
    【解决方案2】:

    点击事件被分配给锚标记a,所以$(this).attr('class');将引用锚标记类属性。所以你应该试试这个而不是这个

    var ourClass =$(this).parent("li").attr('class');

    或者可能只是

    var ourClass =$(this).parent().attr('class');

    【讨论】:

    • 嗨 Dharmesh - 谢谢你的回答 - 这实际上并没有解决我的问题(因为我确实想获得锚的类)但它帮助我重新审视代码并看到问题是“全部”列表项没有“全部”类。我现在已经整理好了。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多