簡體   English   中英

當通過JavaScript知道td的href包含特定文本時如何查找表行ID

[英]How to find table row id when knowing href of td contains specific text by javascript

我有這種格式的行:

   <tr id="_ctl0_viewCompanies_companyRepeater_myresultsRow1_13" class="DGAlternatingItemStyle">
    <td style="padding:0.5em;">
<a style="color: #8B0000; font-size: 1em; font-weight:normal;" href="http://mylinkhere?itemid=12367" target="_blank">some name text</a>
</td>

我知道href中的itemid=12367這部分是唯一的,如何找到包含該itemid的tr的ID? (結果應為: _ctl0_viewCompanies_companyRepeater_myresultsRow1_13 )我嘗試了什么:

 function getAllElementsWithAttribute()
{
  var matchingElements = [];
  var allElements = document.getElementsByTagName('*');
  for (var i = 0, n = allElements.length; i < n; i++)
  {
    if (allElements[i].getAttribute("href"))
    {
      // Element exists with attribute. Add to array.
      matchingElements.push(allElements[i]);
    }
  }
  alert ( matchingElements );
}

但是我不確定從那里還能做什么。

您可以使用包含選擇器的屬性來獲取具有href唯一部分的錨,然后獲取最接近的TR

$('a[href*="itemid=12367"]').closest('tr').prop('id')

小提琴

使用jQuery以選擇器結尾

 $('a[href$="itemid=12367"]').closest('tr').attr('id')

演示

暫無
暫無

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

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