我有以下HTML:
<table class="products">
<tr>
<td>Product description probably goes here</td>
<td><a href="#">more information</a></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
为了让支持Javascript的浏览器更加性感,我添加了这个jQuery(1.3.2):
$(document).ready(function(){
$('table.products tr').click(function(){
$(this).find('a').click();
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
但我认为它会陷入无限循环或其他什么.Safari显示了这个:
RangeError:超出最大调用堆栈大小.jquery.min.js:12
任何人都可以提供解决方法或更好的方法吗?
这里的问题是tr内的谎言.因此,当触发click()方法时,事件会冒泡到包含它的tr,从而创建您提到的递归循环.与其通话.find('a').click(),同时拥有a和tr使用相同的click方法.
$('tr, a').click(function(){
//dostuff
return false;
});
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,我通过添加e.stopPropagation();点击来解决它:
$(document).ready(function() {
$('table.products tr').bind('click', function() {
$(this).find('a').click();
return false;
});
$('table.products tr a').bind('click', function(e) {
e.stopPropagation();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20400 次 |
| 最近记录: |