Href binding orientation in the last TD of the first TR in TBODY

I want to use jQuery to redirect a person arriving on this page to the link contained in the anchor of the last TD of the first TR in TBODY (not THEAD):

<table id="allprogs" class="tablesorter">
<thead>
  <tr>
    <th>Date</th>
    <th>Speaker(s)</th>
    <th>Program Title</th>
    <th>MP3</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>2012: 05/01</td>
    <td>Speaker</td>
    <td><a href="/products/page">Title</a></td>
    <td><a href="http://www.dummy.com">Download</a></td>
  </tr>
  <tr>
    <td>2012: 04/01</td>
    <td>Speaker2</td>
    <td><a href="/products/page2">Title2</a></td>
    <td><a href="http://www.dummy2.com">Download</a></td>
  </tr>

So far my code is:

$(document).ready(function() {
  var url = $('#allprogs tbody tr:first td:last a').attr('href');
  window.location.replace(url);
});

The page load should be redirected to http://www.dummy.com . But it looks like I'm tying the anchor wrong. Suggestions?

+3
source share
2 answers

Your code looks fine. Not sure what exactly you want to achieve, because it looks normal and should do what you want in the beginning. you can try to notify the url of the variable and see if you get the correct url.

Alerts (URL)

Click to see the result:

0

jquery ,

it line window.location.replace(url);, , * .replace, .

:

$(document).ready(function() {
  var url = $('#allprogs tbody tr:first td:last a').attr('href');
  window.location.replace (url);
});

* EDIT:

, , . , .

+1

All Articles