jlyu
11/2/2012 - 7:34 AM

Learning jQuery -9

Learning jQuery -9

(function($) {
    $.extend($.expr[':'], {
        group: function(element, index, matches, set) {
            var num = parseInt(matches[3], 10);
            if (isNaN(num)){
                return false;
            }
            return index % num < num;
        },
        
        containsExactly: function(element, index, matches, set) {
            return $(element).text() === matches[3];
        },
        
        rewriteFilter: function(element, index, matches, set) {
        
        }
    });
})(jQuery);

$(document).ready(function(){
    function stripe() {
        var $news = $('#news');
        $news.find('tr.alt').removeClass('alt').end();
        $news.find('tr.alt-2').removeClass('alt-2').end();
        
        // (1)
        $news.find('tbody').each(function() {
            var $targetRow = $(this).children(':visible').has('td').filter(':group(3)');
            
            $targetRow.filter(function(index){
                return (index % 3)==1;
            }).addClass('alt');
            
            $targetRow.filter(function(index){
                return (index % 3)==2;
            }).addClass('alt-2');
        });
    }
    stripe();
    
    $('#topics a').click(function(){
        var topic = $(this).text();
        var $tpc = $('#topics a:containsExactly("All")');
        
        
        $('#topics a.selected').removeClass('selected');
        $(this).addClass('selected');
        
        $('#news').find('tr').show();
        // (3)
        if (topic != 'All') {
            $('#news').find('tr:has(td)').not(function(){
                return $(this).children(':nth-child(4)').text() == topic;
            }).hide();
        }
        stripe();
        return false;
    });
    /*
    var $cell = $('#release').nextAll().andSelf();
    $cell.addClass('highlight');
    console.log($cell.context);
    console.log($cell.selector);
    console.log($cell.prevObject);
    */
    // (2)
    $('td:containsExactly("jQuery 1.5 Released")').addClass('highlight');
  

});