开发者

Remove link in image inside <a> tag

开发者 https://www.devze.com 2023-01-04 09:53 出处:网络
If I insert an image inside an <a> tag, how could I avoid, using js if it was necessary, that whe开发者_开发问答n someone clicks on that image it doesn\'t follow the link. You need to handle the

If I insert an image inside an <a> tag, how could I avoid, using js if it was necessary, that whe开发者_开发问答n someone clicks on that image it doesn't follow the link.


You need to handle the <img>'s click event and return false;.

For example:

<img src="..." href="..." onclick="return false;" />

Or, using jQuery:

$('a.SomeClass img').click(function() { return false; });

However, the best solution is to move the <img> outside the <a> tag.


Just to add to other answers on this page: you should style an anchor tag with a background image using css instead of using an image tag eg

{background: transparent url('images/myimage.png') width:20px height:20px}


you can also do it by event handling:

$('a.SomeClass img').click(function(event) { 
   event.preventDefault(); 
   return false;
 });

the 'event' object is automatically passed in the 'click' event handler in jQuery. Note that preventDefault() is a native javascript call on the event object, so it can be used w/o jQuery as well.

0

精彩评论

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