我试图在我的网站上包含 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;
});
});
参见这里:http://stakk.it/
错误是什么?
谢谢你,抱歉我的英语不好!
最佳答案
如果 .attr("class")
返回 undefined
,则无法对其调用 .split
,因为 .split
是 String
对象的一个方法,不能在 undefined
上调用。您需要存储 .attr("class")
的结果,然后仅在它不是 undefined
时才拆分它。
var filterClass = $(this).attr('class');
filterClass = filterClass ? filterClass.split(' ').slice(-1)[0] : '';
现在filterClass将包含您期望的内容,或者一个空字符串。
编辑:您可以将 $(this).attr('class')
替换为 this.className
(从已删除的答案中提取)。
关于jquery - 未捕获的类型错误 : Cannot call method 'split' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7987278/