admin管理员组文章数量:1333618
Need a bit of help with a Js error I am getting please:
Uncaught TypeError: $portfolio.isotope is not a function
//ISOTOPE FUNCTION - FILTER PORTFOLIO FUNCTION $portfolio = $('.portfolio-items'); $portfolio.isotope({ itemSelector : 'li', layoutMode : 'fitRows' }); $portfolio_selectors = $('.portfolio-filter >li>a'); $portfolio_selectors.on('click', function(){ $portfolio_selectors.removeClass('active'); $(this).addClass('active'); var selector = $(this).attr('data-filter'); $portfolio.isotope({ filter: selector }); return false; });
Need a bit of help with a Js error I am getting please:
Uncaught TypeError: $portfolio.isotope is not a function
//ISOTOPE FUNCTION - FILTER PORTFOLIO FUNCTION $portfolio = $('.portfolio-items'); $portfolio.isotope({ itemSelector : 'li', layoutMode : 'fitRows' }); $portfolio_selectors = $('.portfolio-filter >li>a'); $portfolio_selectors.on('click', function(){ $portfolio_selectors.removeClass('active'); $(this).addClass('active'); var selector = $(this).attr('data-filter'); $portfolio.isotope({ filter: selector }); return false; });Share Improve this question edited Jun 25, 2015 at 16:25 HaveNoDisplayName 8,517106 gold badges40 silver badges50 bronze badges asked Jun 25, 2015 at 11:32 user2202463user2202463 1351 gold badge2 silver badges12 bronze badges 6
- Has isotope loaded before you run this script? – Turnip Commented Jun 25, 2015 at 11:51
- Yes, Isotope has loaded. But on the page itself it shows no errors, only on pages without the isotope gallery. – user2202463 Commented Jun 25, 2015 at 12:03
-
So you are running this on pages that don't contain any
.portfolio-items
elements? – Turnip Commented Jun 25, 2015 at 12:05 - yes... as the Js is in a global script which is linked on all pages – user2202463 Commented Jun 25, 2015 at 12:06
-
1
Wrap your code form line 2 down in a length check:
if ($portfolio.length) { ...rest of your code... }
this will prevent it from running on pages that don;t contain the required elements – Turnip Commented Jun 25, 2015 at 12:09
1 Answer
Reset to default 4If you don't want your script to run on pages that don't contain the required elements (.portfolio-items
), you can run your script conditionally based on the length
property of your element collection stored in $portfolio
:
$portfolio = $('.portfolio-items');
if ($portfolio.length) { // if 'length' is non zero. Enter block...
$portfolio.isotope({
itemSelector : 'li',
layoutMode : 'fitRows'
});
$portfolio_selectors = $('.portfolio-filter >li>a');
$portfolio_selectors.on('click', function(){
$portfolio_selectors.removeClass('active');
$(this).addClass('active');
var selector = $(this).attr('data-filter');
$portfolio.isotope({ filter: selector });
return false;
});
}
本文标签: javascriptIsotope JS ErrorStack Overflow
版权声明:本文标题:javascript - Isotope JS Error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742275612a2445137.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论