简体   繁体   English

带有链接的可点击行

[英]Clickable row with link

How can I make each row clickable without repeating 如何使每一行都可单击而不重复

This one is an example that shows the problem, parameter could be the code: 这是一个显示问题的示例,参数可以是代码:

<table>
<thead>
    <tr>
        <th>Code</th>
        <th>User</th>
        ...
    </tr>
</thead>
<tbody>
    <tr>
        <td><a href="page/123"> 123 </a></td>
        <td><a href="page/123"> User A </a></td>
        ...
    </tr>
    <tr>
        <td><a href="page/456"> 456 </a></td>
        <td><a href="page/456"> User B </a></td>
        ...
    </tr>
</tbody>

Thanks Excuse me for my English, I hope that you understand the problem. 谢谢您的英语,我希望您能理解这个问题。

There are a few different ways to achieve this. 有几种不同的方法可以实现此目的。 Here are a couple using plain javascript and one using jQuery. 这是一对使用普通javascript的夫妇和一个使用jQuery的夫妇。

Plain JS 普通JS

With plain javascript with just use the onclick parameter. 对于纯JavaScript,只需使用onclick参数即可。 Pretty straight forward. 非常简单。

<table>
<thead>
    <tr>
        <th>Code</th>
        <th>User</th>
        ...
    </tr>
</thead>
<tbody>
    <tr onclick="window.location='page/parameter1';">
        <td> 123 </td>
        <td> User A </td>
        ...
    </tr>
    <tr onclick="window.location='page/parameter2';">
        <td> 456 </td>
        <td> User B </td>
        ...
    </tr>
</tbody>
</table>

jQuery jQuery的

With jQuery you add a class so you can use that as the selector. 使用jQuery,您可以添加一个类,以便可以将其用作选择器。 There is also a data-href parameter that will hold the URL you want the user to go to when they click the row. 还有一个data-href参数,该参数将保存您希望用户单击该行时要转到的URL。

<table>
<thead>
    <tr>
        <th>Code</th>
        <th>User</th>
        ...
    </tr>
</thead>
<tbody>
    <tr class="clickable" data-href="page/parameter1">
        <td> 123 </td>
        <td> User A </td>
        ...
    </tr>
    <tr class="clickable" data-href="page/parameter2">
        <td> 456 </td>
        <td> User B </td>
        ...
    </tr>
</tbody>
</table>

<script>
    jQuery(document).ready(function($) {
        $("tr.clickable").click(function() {
            window.location = $(this).data("href");
        });
    });
</script>

Your code should look like : 您的代码应如下所示:

<table>
<thead>
    <tr>
        <th>Code</th>
        <th>User</th>
        ...
    </tr>
</thead>
<tbody>
    <tr>
        <td><a href="page/parameter1"> 123 </a></td>
        <td><a href="page/parameter1"> User A </a></td>
        ...
    </tr>
    <tr>
        <td><a href="page/parameter2"> 456 </a></td>
        <td><a href="page/parameter2"> User B </a></td>
        ...
    </tr>
</tbody>

Added end tag </a> 添加了结束标记</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM