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

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

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


    
        
            Blah Blah
           


        
26条回答
  •  無奈伤痛
    2020-11-22 14:34

    A much more flexible solution is to target anything with the data-href attribute. This was you can reuse the code easily in different places.

    
        
            Col 1
            Col 2
        
    
    

    Then in your jQuery just target any element with that attribute:

    jQuery(document).ready(function($) {
        $('*[data-href]').on('click', function() {
            window.location = $(this).data("href");
        });
    });
    

    And don't forget to style your css:

    [data-href] {
        cursor: pointer;
    }
    

    Now you can add the data-href attribute to any element and it will work. When I write snippets like this I like them to be flexible. Feel free to add a vanilla js solution to this if you have one.

提交回复
热议问题