Wrapping HTML table rows in tags

后端 未结 9 902
难免孤独
难免孤独 2020-12-03 00:55
相关标签:
9条回答
  • 2020-12-03 01:20

    Just in additional to others I have to add that I personnaly prefer to do like this

    <tr onclick="window.location='http://www.google.com';" >
      <td>Text...</td>
    </tr>
    

    in case of ASP.NET MVC do it like

    <tr onclick="window.location = '@Url.RouteUrl("Product", new { SeName = @product.SeName })';">
    
    0 讨论(0)
  • 2020-12-03 01:24

    as a link in each td is not a good alternative and using js is a bit dirty, here is another html/css approach:

    HTML:

    <div class="table">
        <a class="table-row" href="/mylink">
            <div class="table-cell">...</div>
            <div class="table-cell">...</div>
            <div class="table-cell">...</div>
        </a>
    </div>
    

    CSS:

    .table { display:table; }
    .table-row { display:table-row; }
    .table-cell { display:table-cell; }
    
    0 讨论(0)
  • 2020-12-03 01:25

    It renders like that because the browser is respecting the W3C specification and only allowing <tr> tags as direct descendents of <table>.

    As a solution, you could either put an <a> tag inside each <td> that points to the same URL:

    <table>
        <tr>
            <td>
                <a href="http://url/stuff">
                    First column
                </a>
            </td>
            <td>
                <a href="http://url/stuff">
                    Second column
                </a>
            </td>
        </tr>
    </table>
    

    Or you could bind an onClick handler to the <tr> with JavaScript. A jQuery example would be this:

    $('table tr').click(function() {
        window.location = 'http://location/here';
    });
    

    Or, even better, use delegated events (jQuery 1.7+):

    $('table').on('click', 'tr', function() {
        window.location = 'http://location/here';
    });
    
    0 讨论(0)
提交回复
热议问题