jQuery $elem.hasClass 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22143977/
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
$elem.hasClass is not a function
提问by Ishank Gupta
I found this jsFiddle http://jsfiddle.net/Yvk9q/9/that use isotope for bringing the element I want at the beginning.
I tried to copy that coda on my page but it doesn't work!
I really have no idea why $elem.hasClass is not a function ( I admit I've never used $elem before )
Can anyone help me to solve that?
Thanks!
我发现这个 jsFiddle http://jsfiddle.net/Yvk9q/9/使用同位素在开始时带来我想要的元素。
我试图在我的页面上复制那个结尾,但它不起作用!
我真的不知道为什么 $elem.hasClass 不是一个函数(我承认我以前从未使用过 $elem )
谁能帮我解决这个问题?
谢谢!
jQuery(document).ready(function($) {
var $container = $('#homepage-grid');
$container.imagesLoaded( function(){
$container.isotope({
itemSelector: 'article',
masonry: {
columnWidth: 125
},
getSortData : {
milk : function( $elem ) {
var isMilk = $elem.hasClass('milk');
return (!isMilk?' ':'');
},
eggs : function( $elem ) {
var isEggs = $elem.hasClass('eggs');
return (!isEggs?' ':'');
},
bacon : function( $elem ) {
var isBacon = $elem.hasClass('bacon');
return (!isBacon?' ':'');
}
}
});
});
var $optionSets = $('#main-nav .option-set'),
$optionLinks = $optionSets.find('a');
$optionLinks.click(function(){
var $this = $(this);
// don't proceed if already selected
if ( $this.hasClass('selected') ) {
return false;
}
var $optionSet = $this.parents('.option-set');
$optionSet.find('.selected').removeClass('selected');
$this.addClass('selected');
// make option object dynamically, i.e. { filter: '.my-filter-class' }
var options = {},
key = $optionSet.attr('data-option-key'),
value = $this.attr('data-option-value');
// parse 'false' as false boolean
value = value === 'false' ? false : value;
options[ key ] = value;
if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
// changes in layout modes need extra logic
changeLayoutMode( $this, options )
} else {
// otherwise, apply new options
$container.isotope( options );
}
return false;
});
});
回答by Ishank Gupta
$elem
is not a JQuery object. If you want to use hasClass
(which can only be called on JQuery object) then you should convert it to JQuery object using $($elem)
$elem
不是 JQuery 对象。如果要使用hasClass
(只能在 JQuery 对象上调用),则应使用将其转换为 JQuery 对象$($elem)