jQuery selector: finding table cell based on content
I'm trying to select a table cell which contains specific text but couldn't find out yet how it works: I'm having this code - but doesn't work:
var td = $("td:contains('MyCe开发者_开发知识库ll')",tbl);
Any ideas what's wrong?
The example code below worked for me. Hope it helps....
<script type="text/javascript">
function getCell( cell )
{
var cell || '';
var result = $('tr').find('td:contains('+cell+')');
alert( $(result).text() );
}
<body onload="javascript:getCell('cell 4');">
<table width="30" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>cell 1</td>
<td>cell 2</td>
<td>cell 3</td>
</tr>
<tr>
<td>cell 4</td>
<td>cell 5</td>
<td>cell 6</td>
</tr>
<tr>
<td>cell 7</td>
<td>cell 8</td>
<td>cell 9</td>
</tr>
</table>
</body>
Maybe instead of the second parameter tbl
var td = $("#mySpecificTable td:contains('MyCell')");
Just to make sure, "MyCell" is inside the cell and not its class name or an attribute of the <td>
, right?
Another possibility is the :contains()
selector is case sensitive so if the content is "myCell" the selector wouldn't find it.
have you tried without the ''? as in
var td = $("td:contains(MyCell)",tbl);
精彩评论