Bind onClick event to a .click function
I have an image that I want to execute a link with a javascript onClick event when th开发者_StackOverflow社区e image is clicked.
The link is:
<a class="test" href="javascript:;" onclick="foo.add( 'name=name', 'quantity=1' );">
<img src="pic.png" alt="Click" /></a>
Jquery I am fiddling with
$('.test').click(function(){
onclick = $(this).attr('onclick');
$(this).attr('onclick','');
return false;
});
Not working...
If you really want to just execute an already-existing onclick function, use something like this:
$('a.test img').click(function() {
var link = $(this).parent()[0]
var onclick = $(link).attr('onclick')
onclick.call(link)
})
You want to pass the link into onclick.call. That sets the link as the receiver of the function call, which is how the onclick attribute would normally fire. If you simply call onclick(), you could run into trouble.
加载中,请稍侯......
精彩评论