繁体   English   中英

Jquery:如何在单击tr时触发td a

[英]Jquery: how to trigger td a when tr is clicked

我有一个表,其中trs中的第一个td包含一个链接。 我想要触发(点击)这个锚,无论我在哪里点击包含tr。

我已阅读并尝试了很多关于这个主题的后期和建议,但我不能让这个工作。 我尝试了trigger()和triggerHandle()函数,但它没有触发我想要的锚点击。

必须有其他人在单击tr时需要触发锚点击,这样用户就不必单击tds锚点链接。 如果可能的话,它肯定是一个很好的UI功能?

这是我试过的代码:

<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
    <script type="text/javascript" src="javascripts/jquery-1.4.2.js"></script>
    <script type="text/javascript" charset="utf-8">
        /* Initialise the table with the required column sorting data types */
        jQuery(document).ready(function () {
            jQuery("#rowClick tr").click(function (e) {
                jQuery("#clickevent", this).trigger("click");
            });
        });
    </script> 
</head> 
<body id="dt_example"> 
    <table id="rowClick">
        <thead>
            <tr>
                <th style="width: 30px">id</th>
                <th style="width: 200px">navn</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><a href="datatabletext.asp?test=1" id="clickevent">1</a></td>
                <td>Jesper</td>
            </tr>
            <tr>
                <td><a href="datatabletext.asp?test=2" id="clickevent">2</a></td>
                <td>Bjarne</td>
            </tr>
            <tr>
                <td><a href="datatabletext.asp?test=3" id="clickevent">3</a></td>
                <td>Søren</td>
            </tr>                            
        </tbody>
    </table>
</body> 

继naugtur和亚历山德罗之后; 随着tds改为有一类'clickevent':

$(document).ready(function () {
    $("#rowClick tr").click(function (e) {
        document.location.href = $(this).find(".clickevent").attr('href');
    });
});

由于安全原因,触发点击a以重新加载页面是不可用的。

试试这种方式:

document.location.href=$aintd.attr('href');

$ aintd是您找到的元素

        jQuery("#rowClick tr").click(function (e) {
            jQuery(this).find('td:first a').click();
        });

我应该补充一点,naugtur是正确的,因为这不会带你到另一个页面,但页面上发生的任何事情都可以正常工作。

是的,x1a4是对的。 并从你的tds中删除ids“clickevent”; id属性必须是DOM元素的“主键”。

尝试以下方法

 $("#rowClick tr").click(function (e) {
                $(this).find('td:first a').trigger('click');
            });

暂无
暂无

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

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