2015-06-15 66 views
1

我有一個大單表格,裏面有各種單詞的單元格。我想找到所有帶有「失敗」的單元格作爲文本,在紅色和透明之間開始所有的單元格閃爍,然後停止點擊任何單元格的閃爍(但保持其他單元格一直點擊)。表格元素的閃光顏色

我已經找到一種方法找到與「失敗」每個小區分配一個類給它:

function updateTableColors() { 
    // first 3 rows are headers 
    var numCols = document.getElementById("vimTable").rows[4].cells.length; 

    for(var i = 1; i < numCols; i++) { 
     $('#vimTable td:nth-child(' + i + ')').each(function() { 
      var cellText = $(this).text(); 

      if(cellText === "Failed") { 
       $(this).removeClass(); 
       $(this).addClass("failed"); 
      } 
      else if(cellText === "") { 
          // cell doesn't contain text (could be image) 
       // nothing for now 
      } 
      else { 
       $(this).removeClass(); 
      }     
     }); 
    } 

    return false; 
} 

// Re-run formatting on click 
document.onclick = function() { 
    updateTableColors(); 
} 

我攻擊的計劃是要檢查每個與「失敗」類的元素,然後切換它是否有一個「failed_red」或「failed_transparent」類,並根據它給它一個顏色,但我沒有成功做到這一點,而沒有通過每個元素通過唯一的id。謝謝你的幫助!

注意:我也使用jQuery數據表插件(datatables.net),我的解決方案不能使用HTML5。

編輯:jQuery toggleClass()方法在這裏非常有幫助。

回答

0

要訪問的所有元素與使用jQuery某一類組合:

$('.failed.failed_red').css('background-color', 'red'); 

$('.failed.failed_transparent').css('background-color', 'none'); 
+1

感謝@StaticVoid。我剛剛開始,所以我不能讓你高興,但是一旦我有了更多的聲望,我就會回來並加油。 – lg22woo