admin管理员组

文章数量:1287527

I have a table with rows, that when clicked, take the user to a page with more detailed information about the item that row was describing. Unfortunately, it always changes the current page, and our users would like to be able to middle-mouse/control click the rows in order to open a new tab if they want to. This choice is available with normal links, but not with my onclick it seems. An example is shown below:

<html>
    <body>
        <table border='1'>
        <tr onclick='window.open("")'>
            <td>Open Another Window</td>
        </tr>
        <tr onclick='location.href=""'>
            <td>Change Current Page</td>
        </tr>
        </table>
    </body>
</html>

What is the best way to simulate a normal link with an onclick event so the behaviour will be the same across different os/browsers, which I believe have different bindings for what triggers opening a link in a new tab.

I have a table with rows, that when clicked, take the user to a page with more detailed information about the item that row was describing. Unfortunately, it always changes the current page, and our users would like to be able to middle-mouse/control click the rows in order to open a new tab if they want to. This choice is available with normal links, but not with my onclick it seems. An example is shown below:

<html>
    <body>
        <table border='1'>
        <tr onclick='window.open("http://www.google.")'>
            <td>Open Another Window</td>
        </tr>
        <tr onclick='location.href="http://www.google."'>
            <td>Change Current Page</td>
        </tr>
        </table>
    </body>
</html>

What is the best way to simulate a normal link with an onclick event so the behaviour will be the same across different os/browsers, which I believe have different bindings for what triggers opening a link in a new tab.

Share Improve this question asked Jun 25, 2013 at 8:53 ProgramsterProgramster 12.8k9 gold badges50 silver badges57 bronze badges 2
  • If your table rows only have one cell, why are you using a table for this layout? Is this truly meant to display table data? Anyway this was asked previously. Here's a pretty good answer, but popup blocking will block your tab from opening: stackoverflow./a/11384018/7569308 – jsonp Commented Jan 8, 2020 at 3:08
  • The example only has one cell, but my real-world scenario doesn't. I do have a table of data to show. – Programster Commented Jan 18, 2020 at 12:18
Add a ment  | 

4 Answers 4

Reset to default 3

This works for me:

<tr onclick="window.open('http://www.google.','_blank')">

I just stumbled across this question because I was also looking for a solution. Here is a possible solution. It opens the page normally when clicked normally and in a new tab if the middle button is used.

You can add a table class (like clickable-rows) to easily apply this behavior to your tables. Then add a data attribute (like data-href) with the link.

<!DOCTYPE html>
<html>
    <body>
        <table>
            <tr class="clickable-row" data-href="http://www.google.">
                <td>Clickable row 1</td>
            </tr>
            <tr data-href="http://www.google.">
                <td>Non clickable row</td>
            </tr>
            <tr class="clickable-row" data-href="http://www.google.">
                <td>Clickable row 2</td>
            </tr>
        </table>

        <script>
            // loop through the table rows with the clickable-row class, and turn them into clickable rows by
            // setting the cursor to a pointer, and adding a mousedown listener to open in the current page
            // if left-clicked, or open in a new tab if middle-clicked
            let rows = document.querySelectorAll("tr.clickable-row");

            rows.forEach((clickableRow) => {
                clickableRow.style.cursor = "pointer";

                clickableRow.addEventListener("mousedown", function(e) {
                    e.preventDefault();

                    var href = this.getAttribute('data-href');

                    if (e.button === 1)
                    {
                        window.open(href, "_blank");
                    }
                    else if (e.button === 0)
                    {
                        window.location = href;
                    }
                })
            });
        </script>
    </body>
</html>

Instead of handling the click event of the table row, use an anchor tag. The default behaviour for Ctrl+click for anchors is to open the URL in the href attribute in a new window or tab. If you want a new tab or window opened without the Ctrl button, you can use the target attribute of the anchor as well.

<td><a href="http://www.google." target="_blank">open another window or tab</a></td>

<tr onclick="window.open('./putyourlink.php')"> it's still work to me also but pls put '_blank' like another answer it's good more </tr> 

本文标签: javascriptTable Row onclickchange pageor open new tabStack Overflow