jQuery selector: finding table cell based on content

老子叫甜甜 提交于 2019-12-05 06:08:20

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!