按答案计数过滤问题

时间:2009-07-30 15:14:17

标签: jquery

(关于在StackOverflow上过滤东西的jQuery问题。我发誓,它不属于Meta!)

问题
我希望使用Greasemonkey脚本来过滤Home,Questions和Unanswered页面上的问题。现在我有一个解决方案(下图),但需要几秒钟来处理,我想知道是否有更好的方法。

背景
如果你还没有看一下主页的结构,那就非常相似:
(为问题目的简化)

<div class="question-summary"> 
    <div class="status">
       <div class="mini-counts">1</div>
       <div>answers</div>
    </div>
</div>

这是问题/未答复页面的结构:

<div class="question-summary"> 
    <div class="status">
       <strong>1</strong>
       answers
    </div>
</div>

现在,我需要从每个问题中获取“1”(答案数量),并检查它是否高于某个数字。目前我正在使用此代码:

function filterByAnswer(number)
{
    $.each($('.question-summary'), function()
    {
            // 
    if($('.status .mini-counts',this))
    {
        var answers = $('.status .mini-counts',this).text();
    }
    else if($('.status strong',this))
    {
        var answers = $('.status strong',this).text();
    }
    if(answers > number)
    {
        $(this).hide();
    }       
});
}

我的问题是,有更快的方法吗?我发现这需要几秒钟的时间来处理,并且想要一个更快捷的解决方案。

2 个答案:

答案 0 :(得分:1)

试试这个:

function filterByAnswer(number) 
{
    $('.question-summary').filter( 
        function () { 
            var answers = $(this).find('.status').children('.mini-counts, strong').text(); 
            return parseInt(answers, 10) < number; 
        }).hide();       
}    

答案 1 :(得分:0)

您可以重复使用$('.status .mini-counts',this)$('.status strong',this)的值。

 var $miniCounts = $('.status .mini-counts',this);

 if($miniCounts)
    {
            var answers = $miniCounts.text();
    }
    else 
    {
       var $strong = $('.status strong',this);
       if($strong) {
            var answers = $strong.text();
       }
    }