2013-11-04 147 views
1

是否有任何方法可以選擇僅包含指定文本的td? 我曾嘗試以下:選擇只包含「指定文本」的tds,而不使用jQuery

$("tr td:contains('1')") 

,但它返回一個td的文本有1 td的某個地方爲好。爲了清楚我試圖從下面給出的html得到<td>1</td>,但它一直返回所有這些td s。

<tr> 
    <td>1</td> 
    <td>This contains 1 as well</td> 
    <td><td>And this one contains 1 as well</td> 
</tr> 

有什麼辦法,我可以迫使它返回只有那些td s表示只包含在他們的文字,沒有別的1

+0

可能重複[jQu選擇器 - 匹配內容的元素](http://stackoverflow.com/questions/4673461/jquery-selector-match-content-of-elements) – lonesomeday

回答

1

使用.filter()

$('td').filter(function (i, el) { 
    return this.innerHTML == '1'; 
}).css('background-color','blue'); 
0

有很多選擇,過濾器等

一個簡單的方法是做:

$('td').each(function (i, el) { 
    if (el.innerHTML === '1') { 
     // DO SOMETHING 
     console.log('It has only a 1 in it'); 
    } 
}); 
0

使用.map()

var selectors = $('tr td').map(function() { 
    if (this.innerHTML == '1') { 
     return $(this); 
    } 
}); 
//result: selectors is the td elements with text "1".