jQuery:除了一列之外,使整个表行成为一个链接

Dan*_*Dan 5 jquery html-table clickable

我有一个脚本,可以使每个表行可单击(作为链接),但我需要保持最后一列保持不变,因为此列作为"编辑"按钮.任何人都可以帮我修改脚本,这样它会起作用吗?

这里jQuery到目前为止:

$(document).ready(function() {
  $('#movies tr').click(function() {
    var href = $(this).find("a").attr("href");
    if(href) {
      window.location = href;
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

这是一行的HTML:

<table id="movies">
  <tr class='odd'>
    <td>1</td>
    <td><a href='/film.php?id=1'></a>Tintin</td>
    <td>Tintin and Captain Haddock set off on a treasure hunt for a sunken ship.</td>
    <td><a href='/edit.php?id=1'>edit</a></td>
  </tr>
  .....
Run Code Online (Sandbox Code Playgroud)

Dal*_*len 11

你需要更深入地控制tr元素,将点击处理程序绑定到每个td不是最后一个的处理程序tr:

$(document).ready(function()
{
   $('#movies tr').each(function(i,e)
   {
      $(e).children('td:not(:last)').click(function()
      {
         //here we are working on a td element, that's why we need
         //to refer to its parent (tr) in order to find the <a> element
         var href = $(this).closest("tr").find("a").attr("href");
         if(href)
         {
            window.location = href;
         }              
      });
   });
});
Run Code Online (Sandbox Code Playgroud)