2014-10-10 283 views
0

我該如何去查找一個值爲(foo)的值(文本)並將該行中的下一個單元格設置爲「bar」的表格單元格?jquery搜索表格單元格 - >更新相鄰單元格

要設置可能已經包含了需要被重寫一些文本的單元格。

+1

你有什麼?請顯示一些代碼 – 999k 2014-10-10 15:13:26

回答

1

試試這個,但我haven`t進行了測試。

$('td').filter(function(){ 
    return $(this).text() === 'foo' 
}).next('td').text('bar') 

Demo

0
var rowList = document.getElementById('tableID').children; 
for (var i=0, len = rowList.length; i < len; ++i) { 
    var row = rowList[i]; 
    var cellList = row.children; 
    for (var j=0, size = cellList.length; j < size; ++j) { 
     var cell = cellList[j]; 
     //need additional check to avoid out of bounds issues 
     if (cell.innerHTML == 'foo' && cellList[j + 1]) { 
      cellList[j + 1].innerHTML = 'bar'; 
     } 
    } 
} 

編輯

遺憾錯過了jQuery的標籤。上面的答案是好的,但我相信撥打.text()應該是.html()