未捕获的TypeError:无法调用未定义的方法"split" [英] Uncaught TypeError: Cannot call method 'split' of undefined

查看:263
本文介绍了未捕获的TypeError:无法调用未定义的方法"split"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to include on my website the Quicksand script, but I failed badly.
Firebug gives me this error: 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;
    });
});


参见此处: http://stakk.it/
什么是错误?

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


See here: http://stakk.it/
what is the error?

thank you and sorry for my bad english!

推荐答案

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

If .attr("class") returns undefined, you can't call .split on it because .split is a method of the String object and can't be called on undefined. You need to store the result of .attr("class") and then only split it if it is not undefined.

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

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

now filterClass will contain what you expect, or an empty string.

您可以从已删除的答案中将$(this).attr('class')替换为this.className.

you could replace $(this).attr('class') with this.className, pulled from removed answer.

这篇关于未捕获的TypeError:无法调用未定义的方法"split"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆