简体   繁体   English

如何根据DOM元素的内容在jQuery中引用DOM元素?

[英]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> ? 我怎样才能参考<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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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