jQuery selector: finding table cell based on content

一曲冷凌霜 提交于 2019-12-07 03:57:11

问题


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('MyCell')",tbl);

Any ideas what's wrong?


回答1:


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>



回答2:


Maybe instead of the second parameter tbl

var td = $("#mySpecificTable td:contains('MyCell')");



回答3:


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.




回答4:


have you tried without the ''? as in

var td = $("td:contains(MyCell)",tbl);


来源:https://stackoverflow.com/questions/4082927/jquery-selector-finding-table-cell-based-on-content

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