2010-05-14 122 views
3

我有一个表trs中的第一个td包含一个链接。我希望这个锚点被触发(点击),无论我在哪里点击包含tr。jquery:如何触发tr当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> 

回答

3

继续来自naugtur和Alessandro;与TDS改为有一类 'clickevent' 的:

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

触发一个点击刷新页面是unavaliable由于安全方面的原因。

试试这个方法:

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

$ aintd是一个元素,您发现

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

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

0

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

0

尝试以下

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