如果文本值完全匹配,如何获取td的id

时间:2017-11-17 06:03:57

标签: jquery

如果文本值完全匹配,如何获取td的id?



<table id="tid1">
  <tr>
    <td id="td1">George</td>
    <td id="td2">George2</td>
    <td id="td3">George3</td>
    <td id="td4">Georg</td>

  </tr>
</table>
&#13;
&#13;
&#13;

我想要&#34; Georg&#34; td4返回?

5 个答案:

答案 0 :(得分:2)

您需要使用filter()方法来获取元素。

public static datatable GetResult() {
      var task1 = getResponseFromDb();
      var task2 = getResponseFromThirdParty();
      ProcessResult(task1, task2);
}

public static datatable getResponseFromDB() {
    using(con SqlConnection = new SqlConnection()) {
       //get data from sql
    }
     returns datatable
}

public static datatable getResponseFromthirdParty() {
    //networkrequest
    //process to datatable
    //return datatable
}
console.log(
  // get all td with id attribute
  // or additionally use :contains to get more specific
  // $('td[id]:contains("Georg")')
  $('td[id]')
  // filter the jquery element collection
  .filter(function() {
    // compare content
    return $(this).text().trim() === 'Georg';
  })
  // get the id of filtered element
  .attr('id')
);

答案 1 :(得分:1)

$('#tid1 tr').each(function() {
   $(this).children('td').each(function(j){
        var data =  $(this).html().trim();
        if(data==="Georg"){
           alert("Id of td which contain text 'George' " +$(this).attr('id'));
        }
    });
}); 

答案 2 :(得分:1)

尝试在

下添加扩展功能
 <script>
    $(document).ready(function(){
    alert($("tr").find("td:textEquals('Georg')").attr('id'));
    });

 $.expr[':'].textEquals = function(el, i, m) {
    var searchText = m[3];
    var match = $(el).text().trim().match("^" + searchText + "$")
    return match && match.length > 0;
}
    </script>

答案 3 :(得分:0)

将“Georg”替换为您想要的文字。

$("#tid1 td:contains(Georg)").each(function(){
        alert(this.id);
    });

答案 4 :(得分:0)

$( 'TD:包含( “乔治”)')ATTR(ID)