how to make a whole row in a table clickable as a link?

前端 未结 26 2097
粉色の甜心
粉色の甜心 2020-11-22 14:05

I\'m using Bootstrap and the following doesn\'t work:


    
        
            Blah Blah
           


        
26条回答
  •  情书的邮戳
    2020-11-22 14:30

    This code bellow will make your whole table clickable. Clicking the links in this example will show the link in an alert dialog instead of following the link.

    The HTML:

    Here's the HTML behind the above example:

        
      Name Description Price
    Edit Apples Blah blah blah blah 10.23
    Edit Bananas Blah blah blah blah 11.45
    Edit Oranges Blah blah blah blah 12.56

    The CSS

    And the CSS:

        table#example {
        border-collapse: collapse;   
    }
    #example tr {
        background-color: #eee;
        border-top: 1px solid #fff;
    }
    #example tr:hover {
        background-color: #ccc;
    }
    #example th {
        background-color: #fff;
    }
    #example th, #example td {
        padding: 3px 5px;
    }
    #example td:hover {
        cursor: pointer;
    }
    

    The jQuery

    And finally the jQuery which makes the magic happen:

        $(document).ready(function() {
    
        $('#example tr').click(function() {
            var href = $(this).find("a").attr("href");
            if(href) {
                window.location = href;
            }
        });
    
    });
    

    What it does is when a row is clicked, a search is done for the href belonging to an anchor. If one is found, the window's location is set to that href.

提交回复
热议问题