【问题标题】:Getting a table row to act as a link获取表格行作为链接
【发布时间】:2014-09-05 05:31:37
【问题描述】:

我试图让我的表格行作为链接工作。我在 SO 上找到了一些答案来说明如何做到这一点,但由于某种原因,我的代码无法正常工作。谁能帮我找出导致它不起作用的原因?谢谢。

我试过看 Making a table row (tr) clickable with jQuery (with an a href link, and hover!?)how to get selected table row values using jquery? 到目前为止。

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>X</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <style>
        tr {
            margin:2px;
            padding:10px;
            display:block;
            background:#EEE;
            cursor:pointer;
        }
        tr:hover {
            background:#BBB;
        }
        </style>
    </head>
    <body>


    <script>

    $("tr").click(function() {
      window.location.href = $(this).attr("href");
    });

    </script>

<table class="tbl">
    <tr class=clickableRow href="http://www.zombo.com">
        <td>2014</td>
        <td>ACADIA</td>
        <td>SLE-2</td>
    </tr><tr class=clickableRow href='http://www.xkcd.com'>
        <td>2014</td>
        <td>ACADIA</td>
        <td>Denali</td>
    </tr><tr class=clickableRow href='http://www.legit_url_not_fake_at_all.com'>
        <td>2014</td>
        <td>ACADIA</td>

 ....
 (SNIP)

谢谢。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    使用 Document.ready:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      相关资源
      最近更新 更多