[英]Triggering anchor click within a table using jquery
I am using the below code to trigger the anchor click within the table compareTable
, but it does not seem to take any effect.我正在使用下面的代码来触发表
compareTable
中的锚点点击,但它似乎没有任何效果。 Can any1 point out the solution? any1可以指出解决方案吗?
$('#compareTable a').click(function() {
alert("hi");
});
The <a>
tag doesn't exist at the time you bind that click handler. <a>
标记在您绑定该单击处理程序时不存在。 You can solve this by using .delegate()
or .live()
(or binding the handler when you create the element).您可以通过使用
.delegate()
或.live()
(或在创建元素时绑定处理程序)来解决此问题。 The former is usually considered preferable, but I find you markup difficult, so I'll share a quick workaround with .live()
.前者通常被认为更可取,但我发现你很难标记,所以我将与
.live()
分享一个快速的解决方法。 Simple as can be:尽可能简单:
$('#compareTable a').live('click', function() {
alert("hi");
});
jQuery's methods are two-folded. jQuery 的方法有两个方面。 If you call them with empty arguments (that is, you don't pass any argument), then do what they mean.
如果你用空的 arguments 调用它们(也就是说,你不传递任何参数),那么就按照它们的意思去做。
$('#something').click()
means that it would be clicked. $('#something').click()
表示它会被点击。 If you provide an argument which is usually a callback handler, they just register that handler.如果您提供一个通常是回调处理程序的参数,他们只需注册该处理程序。 So, you should use:
所以,你应该使用:
$('#copareTable a').click();
And of course, since you don't want to click those links without any reason, you probably should write this code in response to another event.当然,既然你不想无缘无故地点击这些链接,你可能应该编写这段代码来响应另一个事件。 Something like:
就像是:
$('#register').click(function(){
$('#compareTable a').click();
});
And also don't forget that $('#comparetTable a')
is a collection of all anchor links inside that table.并且不要忘记
$('#comparetTable a')
是该表内所有锚链接的集合。 So if you send click directive, all of them would be clicked.因此,如果您发送点击指令,它们都会被点击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.