2011-08-01 99 views
1

這是一個簡短的問題。整理If語句

<table id="menuStructuresPageList" class="rounded-corner dataTablePageList"> 

if ($('.dataTablePageList')) { 

我想要做的是做一個聲明,如果說,如果有一個類dataTablesPageList的對象也有menuStructuresPageList隨後的ID ....

但我不知道如何我會完成if語句的尾部。

編輯:

我提出的一點是許多表,這是共享代碼,以便所有的表都具有類dataTablePageList的,但如果其中一人有一個menuStructuresPageList的ID則需要如果做聲明的一部分。

if ($('.dataTablePageList')) { 

} else { 

var oTable = $('.dataTablePageList').dataTable({ 
    "bJQueryUI": true, 
    "iDisplayLength": 10, 
    "sPaginationType": "full_numbers", 
    "aoColumnDefs": [ 
      { "sWidth": "20px", "aTargets": [ 0 ] }, 
      { "sWidth": "40px", "aTargets": [ -1 ] }, 
      { "sWidth": "40px", "aTargets": [ -2 ] }, 
      { "bSortable": false, "aTargets": [ 0 ] }, 
      { "bSortable": false, "aTargets": [ -1] }, 
      { "bSortable": false, "aTargets": [ -2 ] }, 
      { "sClass": "center", "aTargets": [ 0 ] }, 
      { "sClass": "center", "aTargets": [ -1 ] }, 
      { "sClass": "center", "aTargets": [ -2 ] } 
     ] 
}); 
} 
+1

它可以幫助你閱讀css選擇器:http://htmldog.com/guides/cssbeginner/selectors/ –

回答

4

我會用.hasClass

if (!$('#menuStructuresPageList').hasClass('dataTablePageList')) { 
    ... 

編輯:你的意思是你想要的if內的兩個分支?

if ($(this).hasClass('.dataTablePageList')) { 

    // do something 
    if(this.id == "menuStructuresPageList") { 

     // do something 
    } 

} else { 
     ... 
+0

查看我的更新代碼以瞭解我的情況。 –

+1

@Jeff Davidson - 請參閱我的編輯。 (eek,我的重新編輯!我重新編輯!) – karim79

+0

讓我試着再解釋一遍。我所有的表都有dataTablesPageList類。他們全部。但是,如果表的id爲menuStructuresPageList,那麼我希望它做一些事情而不是其他部分。 –

2

嘗試:

if($("#menuStructuresPageList.dataTablesPageList").length){ 
// your code 
} 

if($("#menuStructuresPageList").hasClass("dataTablesPageList")){ 
// your code 
} 
+0

我道歉我沒有更詳細解釋它在一秒鐘內檢查我的更新頁面編輯。 –

+0

閱讀我關於處理多個表的編輯,所以我正在查看該表是否具有特定事物的標識。 –

+1

@Jeff:當我檢查編輯時,Karim已經以最好的方式回答了它。請致電 – Chandu

1

如果你想只同時具有指定的ID和類的對象,那麼你可以把兩者在jQuery選擇規格如下:

if ($('#menuStructuresPageList.dataTablePageList').length > 0) { 
    // found object with id="menuStructuresPageList" and class="dataTablePageList" 
} else { 
    // didn't find object with both class and id as specified 
} 

把它們放在一起在選擇與它們之間沒有空間意味着同一個對象必須同時擁有兩個對象