Make table row clickable

前端 未结 4 513
Happy的楠姐
Happy的楠姐 2021-01-15 19:22

I have a table row that has background color on hover. When user clicks within the background color area, it should grab the link of the anchor tag inside the row and take t

4条回答
  •  清酒与你
    2021-01-15 20:00

    In your case

    $('#ClickableRow').click(function () {
       window.location = $("#ClickableRow a:first").attr('href');
    });
    

    You can test on http://jsfiddle.net/.

    To make it look like clickable you can add this

    JavaScript:

    $('#ClickableRow').hover(
        function () {
            if ($(this).find("th").length > 0) return;
            $(this).addClass("gridRowHover");
        },
        function () { $(this).removeClass("gridRowHover"); }
    );
    

    CSS

    .gridRowHover{
        cursor: pointer;
     }
    

    Generic function, to make any row of table clickable

    function bindRowClick(itemId, eventHandler) {  
        var binder = "tr"; 
        $("#" + itemId).find(binder).click(eventHandler); 
    }
    

    ItemId will be your table id and eventHandler is the function to be executed.

提交回复
热议问题