开发者

Search HTML Table with jQuery

I have a table and need a specific column in a specific row.

<tr></tr>
<tr>
    <td></td>
    <td></td>
    <td class="important_column"><a href="/bla/blah/link">IMPORTANT INFO</a></td>
    <td></td>
    <td class="need_link_here"><a href="/I/WANT/THIS/LINK/">link</a></td>
</tr>
<tr></tr>

So if the link text in "important_column" equals the thing I'm looking for.

Get the link text开发者_JAVA技巧 in "need link_here" column. The text between <a></a>

How to do in jQuery?


You can use :contains() to check to see if an element contains specific text:

$("table td:contains('IMPORTANT INFO')")
    .next().next()
    .children("a");

EXAMPLE - http://jsfiddle.net/Kkywt/


if ($("table tr td.important_column a[href=/bla/blah/link]").length) {
   var link = $("table tr td.need_link_here a").attr("href");
}

variation

$("td.need_link_here a", $("table tr td.important_column a[href=/bla/blah/link]").closest(tr)).attr("href");


Something like this?

$(function() {
  column_text = $(".important_column a").text();
  if(column_text == "IMPORTANT INFO") {
    link_href = $(".need_link_here a").attr("href");
    alert(link_href);
  }
});

Check out this working demo.


It's really not clear what the OP wants. Is this close?

$('#myTable tr').each(function ()
{
    var $tr = $(this);
    if($tr.find('td.important_column').text() === 'IMPORTANT INFO')
    {
        alert($tr.find('td.need_link_here > a').attr('href'))
    }
});


Piece of js that would do the trick:

if($('.important_column a').text() == 'SOME TEXT YOU WANT')
{
   $('.important_column a').attr('href', $('.need_link_here a').attr('href'));
}


You can use the next method to get siblings of a found item:

$('.important_column').next().next().attr("href")

will select the href of the 2nd td element after the one with class="important_column"


jQuery('.need_link_here a',
    jQuery('.important_column a:contains(IMPORTANT INFO)').parent() 
).attr('href');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜