2014-09-04 47 views
0

我想让我的表格行作为链接工作。我已经找到了一些关于如何去做的答案,但由于某种原因,我的代码无法正常工作。任何人都可以帮我找到是什么原因导致它无法工作?谢谢。获取表格行以充当链接

我试图寻找 Making a table row (tr) clickable with jQuery (with an a href link, and hover!?)how to get selected table row values using jquery? 至今。

<!doctype html> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
     <title>X</title> 
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     <style> 
     tr { 
      margin:2px; 
      padding:10px; 
      display:block; 
      background:#EEE; 
      cursor:pointer; 
     } 
     tr:hover { 
      background:#BBB; 
     } 
     </style> 
    </head> 
    <body> 


    <script> 

    $("tr").click(function() { 
     window.location.href = $(this).attr("href"); 
    }); 

    </script> 

<table class="tbl"> 
    <tr class=clickableRow href="http://www.zombo.com"> 
     <td>2014</td> 
     <td>ACADIA</td> 
     <td>SLE-2</td> 
    </tr><tr class=clickableRow href='http://www.xkcd.com'> 
     <td>2014</td> 
     <td>ACADIA</td> 
     <td>Denali</td> 
    </tr><tr class=clickableRow href='http://www.legit_url_not_fake_at_all.com'> 
     <td>2014</td> 
     <td>ACADIA</td> 

.... 
(SNIP) 

谢谢。

回答

2

使用的document.ready:

<script> 
    $(document).ready(function(){ 
     $("tr").click(function() { 
      window.location.href = $(this).attr("href"); 
     }); 
    }); 
</script> 
+0

感谢。这样可行! – CarCzar 2014-09-04 21:32:31