jquery从data-href获取数据

时间:2017-03-12 19:20:26

标签: javascript jquery html

我正在尝试使用jQuery将表行设为链接:

jQuery(document).ready(function() {
    $(".clickable-row").click(function() {
        window.location.href = $(this).data('href');
    });
});

在我的html文件中,我有这个:

<tr class='clickable-row' data-href='my-page.com/user/123'>
    <td><span class="glyphicon glyphicon-user"></span></td>
    <td>{{ member.getFornamn }} {{ member.getEfternamn }}</td>
    <td>{{ member.getEmail }}</td>
    <td>{{ member.getTel1 }}</td>
    <td>{{ member.getPostadress }}</td>
    <td>{{ member.getPostnr }}</td>
</tr>

什么都没发生。 如果我改为:window.location.href ='www.google.com';它正在工作所以我知道 WERE 问题是......

我错过了什么?

修改 Plunker:http://plnkr.co/edit/0MBucaxR1fDpYZjZRLHc?p=preview

出于某种原因,上述情况对我不起作用。如果我使用它,它可以工作:

jQuery(document).ready(function() {
    $(".clickable-row").click(function() {
        window.location.href = '**any link at all**';
    });
});

但是当我改变这个时,我的控制台日志甚至不能识别我的点击......?

jQuery(document).ready(function() {
    $(".clickable-row").click(function() {
        window.location.href = $(this).data('href');
    });
});

1 个答案:

答案 0 :(得分:5)

我这样做了:

jQuery(document).ready(function() {
    $(".clickable-row").click(function() {

        thisdata = $(this).attr('data-href');
        console.log(thisdata);

        window.location.href = thisdata;
    });
});

控制台给了我正确答案。

我注意到我的实际表格看起来像这样:

<tr class='clickable-row' data-href='URL://my-page.com/user/123&#39;&GT;

因此,删除网址:// 就可以了。

向你致以帮助!