Click Entire Row (preserving middle click and ctrl+click)

后端 未结 10 1160
独厮守ぢ
独厮守ぢ 2020-12-07 20:49

I have an HTML table with a link in the first column. I want to allow the user to click anywhere in the row to activate that link. At the same time, I would like to preser

相关标签:
10条回答
  • 2020-12-07 21:12

    I would attack this from the HTML/css side. This used to be a common problem when most sites did all layout in tables.

    First make the contents of all table cells into links. If you don't want them to look like links you can use CSS to remove the underline from the 'non link' cells. But they will be links, which is semantically what you want anyway.

    Next you want to make the link expand to fill the entire cell. StackOverflow already knows the answer to this:

    td a { display: block; width: 100%; height: 100%; line-height: 100%; }

    With a typical table with no spaces between the cells the entire row will be clickable. And since this relies on no tricks or browser specific hacks it should work everywhere.

    0 讨论(0)
  • 2020-12-07 21:17

    EDIT

    This is simple problem that has a simple solution. I don't see a need for nasty hacks that might break on some browsers or take processing time. Especially because there is a neat and easy CSS solution.

    First here is a demo

    Inspired by @Nick solution for a very similar issue, I'm proposing a simple css+jquery solution.

    First, here is the mini-plugin I wrote. The plugin will wrap every cells with a link:

    jQuery.fn.linker = function(selector) {
        $(this).each(function() {
            var href = $(selector, this).attr('href');
            if (href) {
                var link = $('<a href="' + $(selector, this).attr('href') + '"></a>').css({
                    'text-decoration': 'none',
                    'display': 'block',
                    'padding': '0px',
                    'color': $(this).css('color')
                })
                $(this).children()
                       .css('padding', '0')
                       .wrapInner(link);
            }
        });
    };
    

    And here is a usage example:

    $('table.collection tr').linker('a:first');
    

    And All the CSS you need:

    table.collection {
        border-collapse:collapse;
    }
    

    It's as simple as that.


    You can use the event object to check the mouse click type. This article is discussing a similar issue.

    Anyway, here is how to do it:

    $("table#row_link tbody tr").click(function () {
    
        if((!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1)){
            if (!e.ctrlKey) {
                // Left mouse button was clicked without ctrl
                window.location = $(this).find("a:first").attr("href");
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-07 21:22

    Unfortunately there is no way to simulate a link and all associated behaviour in every browser. Therefore, the only way to achieve what you want is to have a link that follows the cursor around the <tr> element; this link would be invisible so, to the user, it looks like they're clicking on the <tr> but they're actually clicking on a hidden link. Using this method, the middle-button, ctrl+click and any other behaviours are left intact!

    Here's a DEMO: http://jsbin.com/ufugo

    And here's the code:

    $("table tr").each(function(){
    
        var $link = $('a:first', this).clone(true),
            dim = {
                x: [
                    $(this).offset().left,
                    $(this).offset().left + $(this).outerWidth()
                ],
                y: [
                    $(this).offset().top,
                    $(this).offset().top + $(this).outerHeight()
                ]
            }
    
        $link
            .click(function(){
                $(this).blur();
            })
            .css({
                position: 'absolute',
                display: 'none',
                // Opacity:0  means it's invisible
                opacity: 0
            })
            .appendTo('body');
    
        $(this).mouseover(function(){
            $link.show();
        });
    
        $(document).mousemove(function(e){
            var y = e.pageY,
                x = e.pageX;
            // Check to see if cursor is outside of <tr>
            // If it is then hide the cloned link (display:none;)
            if (x < dim.x[0] || x > dim.x[1] || y < dim.y[0] || y > dim.y[1]) {  
                return $link.hide();
            }
            $link.css({
                top: e.pageY - 5,
                left: e.pageX - 5
            })
        });
    
    });
    

    EDIT:

    I created a jQuery plugin using a slightly better aproach than above: http://james.padolsey.com/javascript/table-rows-as-clickable-anchors/

    0 讨论(0)
  • 2020-12-07 21:23

    I think the biggerlink plugin will do what you ask for. Here's the

    • article, and
    • demo too.
    0 讨论(0)
  • 2020-12-07 21:25

    you need to remove the < tbody > tag

    and just use the 'href' attribute to get the link destination and dont to select the anchor < a > tag too because thats contains the href attribute.

    $("table#row_link tbody tr a").click(function () {
    
         window.location = $(this).attr("href");
    
    });
    

    or simply make the link open a new tab.

    i hope that helps you.

    0 讨论(0)
  • 2020-12-07 21:28

    You can make a link and let it floting around in your tr, biding to mouveover event, update href and position

    create one pixel link

    <table id="row_link">....</table>
    <a id="onepixel" style="position:absolute;z-index:1000;width:1px;height:1px;"></a>
    

    update href and position on mouse over

    $("#row_link tr").mouseover(
       function(event){
          //update href
          $("#onepixel").attr("href",$(this).find("a:first").attr("href"));
          //update position, just move to current mouse position
          $("#onepixel").css("top",event.pageY).css("left",event.pageX);
       }
    );
    
    0 讨论(0)
提交回复
热议问题