2013-11-22 36 views
1

我過濾頁面上的元素,然後檢查顯示的項目數,如果少於一定數量,我想使用$ .get()加載更多項目。JQuery - 將'HTMLDivElement'對象數組轉換爲字符串

我正在使用同位素插件,它要求新項目是一個字符串,但我似乎只能得到HTMLDivElement對象。如何將其轉換爲字符串?

   var $container = $('#container'), 
         filters = {}; 
       $container.isotope({ 
        itemSelector: '.element', 
       }); 

       function loadMoreItems(getQuery) { 
        var newItems = []; 
        $.get(getQuery, null, function(data) { 
         container = $($container, data).eq(0); 
         if (0 === container.length) { 
          // incase the element is a root element (body > element), 
          // try to filter it 
          container = $(data).filter($container).eq(0); 
         } 

         if (container) { 
          container.find('.element').each(function() { 
           newItems.push(this); 
          }); 
         } 

         alert(newItems); //what to do to get this as a string?? 


        }, 'html'); 
        $container.isotope('insert', newItems, true); 
       } 

回答

1

不熟悉同位素,但如果你需要在一個字符串中的所有元素的HTML,你可以簡單地$(elements).html()或者如果你需要每個元素作爲一個字符串數組,你可以做

var transformElements = []; 
$.each($(elements), function(index, value){ 
    transformElements.push($(value).html()); 
}) 

在您的特定情況下,你可以這樣做:

var newItems = ""; 
if (container) { 
    newItems = container.find('.element').html(); 
} 

,這將造成與所有元素的HTML一個字符串。

+0

感謝您的迴應,但是,我該如何準確地寫出這個?我已經嘗試過各種方法來適應我的代碼,但似乎沒有任何工作。我只需要一個很長的字符串,而不是一個數組。我試圖做this.html()而不是newItems.push(this);但我收到錯誤 – LeeTee

+0

看到更新的答案。 – TigOldBitties