簡體   English   中英

jQuery查找文本並從子元素中提取ID

[英]jQuery find text and extract id from sub element

我正在嘗試在我的所有div中搜索某個文本“ www.url.com”

如果找到www.url.com,我想從子表中提取表ID。

我的HTML是這樣的:

<div class="box" id="results">
   <div class="box-header">
      <h1>www.url.com</h1>
   </div>
   <table cellpadding="0" cellspacing="0" border="0" class="display" id="24">
      <thead>
         <tr>
            <th>col</th>
         </tr>
         <tr id="161">
            <td class="kw" style="border-left: 1px solid white;border-right: 1px solid #F4F4F4;padding: 12px 14px;position: relative;text-align: left;">111111</td>

         </tr>
      </thead>
      <tbody></tbody>
   </table>

在這種情況下,我想找到id =“ 24”。

這是我到目前為止所擁有的:

var foundin = $('*:contains("www.url.com")');

不知道如何獲取ID :)

采用:

foundin.find('table').attr('id');

如果您想收集所有實例,請使用以下方法:

foundin.each(function(){
    $(this).find('table').attr('id'); // Gets the ID
    // Store it into something like an array
});

嘗試一下,如果所有表看起來都一樣,則應該可以使用。

 $('*:contains("www.url.com")').find('table:first').attr('id');
var tableId = $('*:contains("www.url.com")').closest('.box').find('table').attr('id');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM