2010-01-24 74 views
2

嘿,我想爲下面的腳本動態地獲取類名。如何獲取類名?

我有不同的類名是通過PHP創建的,所以需要動態地使用jQuery來獲取它們。

正如您在下面看到的,似乎有2個區域需要獲取類名。目前,它是硬編碼的(班被命名爲 '結果'):

1)VAR NEW_CONTENT = $( '#hiddenresult div.result:EQ( '+ + PAGE_INDEX')')的clone() ;

2)var num_entries = $('#hiddenresult div.result').length;

所以,我想讓jquery獲取類名,而不是我剛剛編碼它,如下所示。

jQuery的文件:

<script type="text/javascript"> 

     function pageselectCallback(page_index, jq){ 
     var new_content = $('#hiddenresult div.result:eq('+page_index+')').clone(); 
     $('#Searchresult').empty().append(new_content); 
     return false; 
    } 

    /** 
    * Callback function for the AJAX content loader. 
    */ 
    function initPagination() { 
     var num_entries = $('#hiddenresult div.result').length; 
     // Create pagination element 
     $("#Pagination").pagination(num_entries, { 
      num_edge_entries: 2, 
      num_display_entries: 8, 
      callback: pageselectCallback, 
      items_per_page:1 
     }); 
    } 

    // Load HTML snippet with AJAX and insert it into the Hiddenresult element 
    // When the HTML has loaded, call initPagination to paginate the elements   
    $(document).ready(function(){  
     initPagination(); 
    }); 
</script> 

上實現解決方案的任何幫助將是巨大的。謝謝

+0

你可以發佈你的HTML標記的樣本嗎? – 2010-01-24 00:47:53

回答

1

如果我理解正確,你希望你的代碼工作,無論給予div的類。 (但有些班級將給予)

如果是使用

var new_content = $('#hiddenresult div[class]:eq('+page_index+')').clone(); 

var num_entries = $('#hiddenresult div[class]').length; 

這意味着找到具有已定義的類屬性(下#hiddenresult任何DIV不管實際的類名稱..)

+0

非常感謝,完美的工作。 – 2010-01-24 01:22:20

+1

在相關說明中,我建議使用'.eq(page_index)'函數而不是':eq'選擇器。在我的博客的解釋在這裏:http://spadgos.com/?p=51 – nickf 2010-01-24 01:34:53

+0

尼斯抓到尼克,謝謝你的頭。 – 2010-01-24 01:58:34

0

您可以像這樣獲取類屬性的內容:
$("#hiddenresult div").attr("class");
注意:如果您的元素有多個類分配給它,這可能不適用於您。

您也可以使用此:
$("#hiddenresult div").hasClass("result");
要檢查元素是否有特定的類。