开发者

Bind onClick event to a .click function

开发者 https://www.devze.com 2023-01-23 15:34 出处:网络
I have an image that I want to execute a link with a javascript onClick event when th开发者_StackOverflow社区e image is clicked.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消