dtjkl42086 2013-11-12 19:57
浏览 86

循环遍历所有可用的css属性以获得计数?

I have a list of database results, each tagged with css properties for filtering purposes. Some could have many tags, some few.

    <ul class="results">
      <li class="filterThis property propertysome property other propertyetc>
        <!— item info —> 
      </li>
      <!— next item —>
    </ul>

This line counts the visible number of results that have one particular css property ('.property' in this case).

    var numProperty = $('.filterThis.property:visible').length

And this updates the number in the list of filters, which are loaded after the results are loaded

    $(".whenReady label span.property”).html(numProperty);

How do I extend this to loop through all available css properties, count them as above, and update the html as above?

Each() seems like the way to go, but I can't seem to get the structure or naming right to either produce results at all, or to carry the names all the way through so I can assign the html updates.

All the possible properties are available as php results, so that could be a json object to jQuery (or something else?), but I’m at a loss of how to take the next steps and get the desired functionality.

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanping2695 2013-11-12 20:10
    关注

    Couldn't you fix this in the backend? For example, keep track of the available filters:

    <?php
      $results = .... ;
      $filters = array();
    
      foreach($result in $results){
        // assuming each result has an array of filters
        $filters = array_merge($filters, $result['filter']);
      }
    
      $filters = array_unique($filters);
      $csFilters = implode(',',$filters);
    ?>
    

    With the example above, you have a comma seperated string with available filters which you can then apply as a data-attribute on your results list:

    <ul class="results" data-filters="a,b,c,d">
    

    Now you can access theses filters in jQuery:

    $('.results').data('filters').split(',');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染