Link entire table row?

后端 未结 8 1227
后悔当初
后悔当初 2020-12-01 03:43

I know it is possible to link an entire table cell with CSS.

.tableClass td a{
   display: block;
}

Is there a way to apply a link to an en

相关标签:
8条回答
  • 2020-12-01 03:55

    Example: http://xxjjnn.com/linktablerow.html

    Link entire row:

    <table>
      <tr onclick="location.href='SomeWherrrreOverTheWebsiiiite.html'">**
        <td> ...content... </td>
        <td> ...content... </td>
        ...
      </tr>
    </table>
    

    Iff you'd like to do highlight on mouseover for the entire row, then:

    <table class="nogap">
      <tr class="lovelyrow" onclick="location.href='SomeWherrrreOverTheWebsiiiite.html'">**
         ...
      </tr>
    </table>
    

    with something like the following for css, which will remove the gap between the table cells and change the background on hover:

    tr.lovelyrow{
      background-color: hsl(0,0%,90%);
    }
    
    tr.lovelyrow:hover{
      background-color: hsl(0,0%,40%);
      cursor: pointer;
    }
    
    table.nogap{
      border-collapse: collapse;
    }
    

    Iff you are using Rails 3.0.9 then you might find this example code useful:

    Sea has many Fish, Fish has many Scales, here is snippet of app/view/fish/index.erb

    <table>
    <% @fishies.each do |fish| %>
      <tr onclick="location.href='<%= sea_fish_scales_path(@sea, fish) %>'"> 
        <td><%= fish.title %></td>
      </tr>
    <% end %>
    </table>
    

    with @fishies and @sea are defined in app/controllers/seas_controller.rb

    0 讨论(0)
  • 2020-12-01 03:55

    I think this might be the simplest solution:

    <tr onclick="location.href='http://www.mywebsite.com'" style="cursor: pointer">
    <td>...</td>
    <td>...</td>
    </tr>
    

    The cursor CSS property sets the type of cursor, if any, to show when the mouse pointer is over an element.

    The inline css defines that for that element the cursor will be formatted as a pointer, so you don't need the 'hover'.

    0 讨论(0)
  • 2020-12-01 03:56

    Also it depends if you need to use a table element or not. You can imitate a table using CSS and make an A element the row

    <div class="table" style="width:100%;">
      <a href="#" class="tr">
        <span class="td">
          cell 1
        </span>
        <span class="td">
          cell 2
        </span>
      </a>
    </div>
    

    css:

    .table{display:table;}
    .tr{display:table-row;}
    .td{display:table-cell;}
    .tr:hover{background-color:#ccc;}
    
    0 讨论(0)
  • 2020-12-01 04:01

    Use the ::before pseudo element. This way only you don't have to deal with Javascript or creating links for each cell. Using the following table structure

    <table>
      <tr>
        <td><a href="http://domain.tld" class="rowlink">Cell</a></td>
        <td>Cell</td>
        <td>Cell</td>
      </tr>
    </table>
    

    all we have to do is create a block element spanning the entire width of the table using ::before on the desired link (.rowlink) in this case.

    table {
      position: relative;
    }
    
    .rowlink::before {
      content: "";
      display: block;
      position: absolute;
      left: 0;
      width: 100%;
      height: 1.5em; /* don't forget to set the height! */
    }
    

    demo

    The ::before is highlighted in red in the demo so you can see what it's doing.

    0 讨论(0)
  • 2020-12-01 04:04

    I agree with Matti. Would be easy to do with some simple javascript. A quick jquery example would be something like this:

    <tr>
      <td><a href="http://www.example.com/">example</a></td>
      <td>another cell</td>
      <td>one more</td>
    </tr>
    

    and

    $('tr').click( function() {
        window.location = $(this).find('a').attr('href');
    }).hover( function() {
        $(this).toggleClass('hover');
    });
    

    then in your CSS

    tr.hover {
       cursor: pointer;
       /* whatever other hover styles you want */
    }
    
    0 讨论(0)
  • 2020-12-01 04:17

    Unfortunately, no. Not with HTML and CSS. You need an a element to make a link, and you can't wrap an entire table row in one.

    The closest you can get is linking every table cell. Personally I'd just link one cell and use JavaScript to make the rest clickable. It's good to have at least one cell that really looks like a link, underlined and all, for clarity anyways.

    Here's a simple jQuery snippet to make all table rows with links clickable (it looks for the first link and "clicks" it)

    $("table").on("click", "tr", function(e) {
        if ($(e.target).is("a,input")) // anything else you don't want to trigger the click
            return;
    
        location.href = $(this).find("a").attr("href");
    });
    
    0 讨论(0)
提交回复
热议问题