未捕获的TypeError:无法调用未定义的方法'split'

hum*_*ing 7 jquery

我试图在我的网站上加入Quicksand脚本,但我失败了.
Firebug给了我这个错误65 Uncaught TypeError: Cannot call method 'split' of undefined:

对于这个脚本:

jQuery.noConflict();
jQuery(document).ready(function($){
    // Clone applications to get a second collection
    var $data = $("#portfolio-items").clone();

    //NOTE: Only filter on the main portfolio page, not on the subcategory pages
    $('#portfolio-terms ul li').click(function(e) {
        $("ul li").removeClass("active");   
        // Use the last category class as the category to filter by. This means that multiple categories are not supported (yet)
        var filterClass=$(this).attr('class').split(' ').slice(-1)[0];
jquery.custom.js:65 Uncaught TypeError: Cannot call method 'split' of undefined (repeated 6 times)

        if (filterClass == '.all current') {
            var $filteredData = $data.find('#portfolio-');
        } else {
            var $filteredData = $data.find('#portfolio-[data-type=' + filterClass + ']');
        }
        $("#portfolio-items").quicksand($filteredData, {
            duration: 800,
            easing: 'swing',
        });     
        $(this).addClass("active");             
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)


看到这里:http ://stakk.it/
错误是什么?

谢谢你,抱歉我的英语不好!

use*_*654 18

如果.attr("class")返回undefined,则无法调用.split它,因为它.splitString对象的方法而无法调用undefined.您需要存储结果,.attr("class")然后仅在不存在时将其拆分undefined.

var filterClass = $(this).attr('class');
filterClass = filterClass ? filterClass.split(' ').slice(-1)[0] : '';
Run Code Online (Sandbox Code Playgroud)

现在filterClass将包含您期望的内容或空字符串.

编辑:您可以取代$(this).attr('class')使用this.className,移除答案拉动.