jQuery 基础教程(第3版) ---第九章习题答案

//第一题
$(document).ready(function(){
    function stripe(){
        $('#news').find('tr.alt').removeClass('alt');
        $('#news tbody').each(function(){
            $(this).children(':visible').has('td').filter(
                function(index){
                    return (index%3)==1;
                }
            ).addClass('alt').end().filter(
                function(index){
                    return (index%3)==2;
                }
            ).addClass('alt-2');
        });
    }
    
    stripe();
    
    
    $('#topics a').click(function(){
        var topic = $(this).text();
        
        $('#topics a.selected').removeClass('selected');
        $(this).addClass('selected');
        
        $('#news').find('tr').show();
        if(topic!='All'){
            $('#news').find('tr:has(td)').not(function(){
                return $(this).children(':nth-child(4)')
                        .text()==topic;
            
            }).hide();
        }
        
        stripe();
        return false;
        
    });
});

//第二题
(function($){
    $.extend($.expr[':'],{
        containsExactly:function(element,index,matches,set){
            var value = matches[3];
            var x = $(element).text();
            if(value!=x){
                return false
            }
            return true;
        }
    });
})(jQuery);
//可使用的调用方法
$('#news td').click(function(){
    $(this).filter(':containsExactly("XXX")').addClass('YYY');
});


//第三题
if(topic!='All'){
    //原代码
    $('#news').find('tr:has(td)').not(function(){
        return $(this).children(':nth-child(4)').text()==topic;
    }).hide();
    
    //新的
    $('#news').find('tr:has(td)').not(':containsExactly('+topic+')').hide();
    
} 
//第四题
(function($){
    $.fn.grandparent=function(){
        var $current=$();
        this.each(function(){
            $current = $(this).parents();
        });
        return this.pushStack($current);
    }
})(jQuery);
原文地址:https://www.cnblogs.com/wanlxz/p/3477998.html