jQuery 未捕获的类型错误:无法调用未定义的方法“拆分”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7987278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot call method 'split' of undefined
提问by humanbeing
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
:
我试图在我的网站上包含 Quicksand 脚本,但我失败了。
Firebug 给了我这个错误65 Uncaught TypeError: Cannot call method 'split' of undefined
:
for this script:
对于这个脚本:
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;
});
});
See here: http://stakk.it/
what is the error?
thank you and sorry for my bad english!
请参阅此处:http: //stakk.it/
错误是什么?
谢谢你,对不起我的英语不好!
回答by Kevin B
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
.
如果.attr("class")
返回undefined
,则不能调用.split
它,因为它.split
是String
对象的方法并且不能被调用undefined
。您需要存储 的结果,.attr("class")
如果不是,则仅将其拆分undefined
。
var filterClass = $(this).attr('class');
filterClass = filterClass ? filterClass.split(' ').slice(-1)[0] : '';
now filterClass will contain what you expect, or an empty string.
现在 filterClass 将包含您期望的内容,或一个空字符串。
Edit: you could replace $(this).attr('class')
with this.className
, pulled from removed answer.
编辑:您可以替换$(this).attr('class')
为this.className
, 从已删除的答案中拉出。
回答by Yuseferi
Another solution is using toString
method
另一种解决方案是使用toString
方法
var filterClass = $(this).attr('class').toString();
filterClass = filterClass.split(' ');
it worked for me
它对我有用