最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting the href from <a> tag within tables - Stack Overflow

programmeradmin2浏览0评论

I'm writing a Greasemonkey script, which redirects the user if there's only one search result to that one page. I'm trying to get the link from a table, which has many parent tables. That part of the page that I'm trying to get the link from looks like this:

<tr class="odd">
<td class="name first">
    <a href="need this one">Text</a>
</td>

Tried document.getElementById('name first'), but still no success. I need help without any external libraries. The page has a design of darkening the background of even cells, so if there's no class called 'even' then it assumes that there's only one result, and that part works correctly.

I'm writing a Greasemonkey script, which redirects the user if there's only one search result to that one page. I'm trying to get the link from a table, which has many parent tables. That part of the page that I'm trying to get the link from looks like this:

<tr class="odd">
<td class="name first">
    <a href="need this one">Text</a>
</td>

Tried document.getElementById('name first'), but still no success. I need help without any external libraries. The page has a design of darkening the background of even cells, so if there's no class called 'even' then it assumes that there's only one result, and that part works correctly.

Share Improve this question asked Mar 11, 2011 at 22:27 ExecExec 4077 silver badges18 bronze badges 4
  • 1 class != id Nathan's answer is a pretty reasonable solution. – Endophage Commented Mar 11, 2011 at 22:36
  • Well, it is something wrong in my code, as the variable 'a' always returns undefined, can't even use the .getAttribute('href') with it. Actually, I tried window.location(a.getAttribute('href')), but I can't seem to understand what's wrong with it. – Exec Commented Mar 11, 2011 at 23:00
  • Post the exact code you are using, in full. Nathan's answer is good for the question as stated. If you are trying to follow the link, You would use window.location.href = a.href; or window.location.replace (a.href);. – Brock Adams Commented Mar 12, 2011 at 1:46
  • @Brock Adams: The code consists of a function, which checks the existence of class Even, from this question, and after declaring the function: if ( checkExistOfEven() == false ) { var td = document.getElementsByClassName('name first')[0]; var a = td.getElementsByTagName('a')[0] window.location.replace (a.href); The site for I'm trying to build the script is this – Exec Commented Mar 12, 2011 at 7:28
Add a ment  | 

3 Answers 3

Reset to default 5

You are probably safe to use getElementsByClassName, as this is a Greasemonkey script and those browsers probably support it:

var td = document.getElementsByClassName('name first')[0];
var a = td.getElementsByTagName('a')[0];

Based on the target page and your apparent purpose (follow the first result link, if there are is only one search result), the code you want is this:

var EvenRow = document.querySelector ("table.listing tr.even");
if (EvenRow == null)
{
    var ResultLink  = document.querySelector ("table.listing tr.odd td.name.first a");
    if (ResultLink != null)
        window.location.href = ResultLink.href;
}


Notes:

  1. There are multiple nodes with both the "name" and "first" classes. This is the reason for the more specific selector. It's also the reason why Nathan's answer failed (he ignored the tr.odd, for some reason).

  2. A checkExistOfEven() function is not needed.

  3. In this case, you probably want window.location.href = rather than window.location.replace(). The former preserves the search-results page in the history, in case there is some hiccup.

using document.querySelector is probably easier:

var a = document.querySelector("td.first.name a");
发布评论

评论列表(0)

  1. 暂无评论