简体   繁体   中英

How do I get reference to DOM element in jQuery based on DOM element's contents?

Consider this example:

<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>    
  </tr>  
</table>

How can I get reference to <td>2</td> ? I need reference as I want to trigger event on it.

Just loop like:

 $('table td').each(function() { if ($(this).text() === '2') { // trigger some event $(this).doTheThing(); } }); 

$('td:contains("2")')

选择内部编号为2的td。

.filter()

Description: Reduce the set of matched elements to those that match the selector or pass the function's test.

$("td").filter(function() {
    return $(this).text() === "2";
}).trigger("whatever");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM