最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable click on img tag under anchor tag - Stack Overflow

programmeradmin3浏览0评论

I have an img tag under anchor tag, i dont want click on img tag, i tried disabled property, it seems there is no such property for img tag.Please let me know how to disable click on img.

code:

<a href="google"><img src="sample.png"></img></a>

Thankyou

I have an img tag under anchor tag, i dont want click on img tag, i tried disabled property, it seems there is no such property for img tag.Please let me know how to disable click on img.

code:

<a href="google."><img src="sample.png"></img></a>

Thankyou

Share Improve this question edited Feb 17, 2014 at 4:47 Java Learner asked Feb 14, 2014 at 6:35 Java LearnerJava Learner 3213 gold badges10 silver badges26 bronze badges 2
  • The click event is on the <a> element, why not just remove the hyperlink? – Jason Sperske Commented Feb 14, 2014 at 6:37
  • @RajasekharP its working check the fiddle demo jsfiddle/gKb6B – Shaik Mahaboob Basha Commented Feb 14, 2014 at 7:14
Add a ment  | 

3 Answers 3

Reset to default 3

With CSS, you can use pointer-events property:

img {
    pointer-events: none;
    cursor: default;
}

You can check browser support for this property here.

Btw, <img></img> is not valid HTML markup. You need to use <img />

Fiddle Demo

Check this question, is it the thing you want? Disable link using css

So that your code will be...

<a href="google." style="pointer-events: none;  cursor: default;">
    <img src="sample.png" />
</a>

By the way, in HTML the img tag has no end tag. http://www.w3schools./TAGS/tag_img.asp

To prevent the default behavior we can use event.preventDefault() method of the event. Older browsers (ie 7 and below) dont support this method, so we have to use onclick="return false;"

Note:

I added simple text with in the link, to demonstrate the image click is disabled but when we click the text, the anchor click gets activated.

function onimgclick(event) {
    if(event.preventDefault)
        event.preventDefault();
    else {
        return false;
    }
}

And the html markup should be

<a href="google."><img onclick="return onimgclick(event)" src="sample.png"></img> HI</a>
发布评论

评论列表(0)

  1. 暂无评论