I am using the below code to trigger the anchor click within the table compareTable
, but it does not seem to take any effect. Can any1 point out the solution?
$('#compareTable a').click(function() {
alert("hi");
});
开发者_如何学C
Here is the demo
The <a>
tag doesn't exist at the time you bind that click handler. You can solve this by using .delegate()
or .live()
(or binding the handler when you create the element). The former is usually considered preferable, but I find you markup difficult, so I'll share a quick workaround with .live()
. Simple as can be:
$('#compareTable a').live('click', function() {
alert("hi");
});
jQuery's methods are two-folded. If you call them with empty arguments (that is, you don't pass any argument), then do what they mean. $('#something').click()
means that it would be clicked. 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. So if you send click directive, all of them would be clicked.
精彩评论