2011-11-02 155 views
7

我想在我的網站上包含流沙腳本,但是我失敗了。
螢火蟲給我這個錯誤:65 Uncaught TypeError: Cannot call method 'split' of undefined
未捕獲TypeError:無法調用未定義的方法'split'

這個腳本:

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/
什麼錯誤?

謝謝你,對不起我的英文不好!

+0

你爲什麼會認爲filterClass可能可能等於'。所有current'? – Neil

回答

18

如果.attr("class")回報undefined,你不能稱之爲.split它,因爲.splitString對象的方法,不能在undefined調用。您需要存儲.attr("class")的結果,然後只在不是undefined時將其拆分。

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

現在filterClass將包含您期望的內容或空字符串。

編輯:你可以用this.className代替$(this).attr('class'),從已刪除的答案中拉出。

+0

非常感謝您的回答。現在我解決了這個問題,但我不明白爲什麼圖像會消失。你可以在這裏看到它:http://stakk.it/ – humanbeing

0

另一種解決方案是使用toString方法

var filterClass = $(this).attr('class').toString(); 
    filterClass = filterClass.split(' '); 

它爲我工作

相關問題