简体   繁体   English

禁用点击锚标签下的img标签

[英]Disable click on img tag under anchor tag

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. 我在锚标签下有一个img标签,我不想单击img标签,我尝试了禁用属性,看来img标签没有这种属性。请让我知道如何禁用img单击。

code: 码:

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

Thankyou 谢谢

With CSS, you can use pointer-events property: 使用CSS,您可以使用指标事件属性:

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

You can check browser support for this property here . 您可以在此处检查浏览器对此属性的支持。

Btw, <img></img> is not valid HTML markup. 顺便说一句, <img></img>是无效的HTML标记。 You need to use <img /> 您需要使用<img />

Fiddle Demo 小提琴演示

To prevent the default behavior we can use event.preventDefault() method of the event. 为了防止出现默认行为,我们可以使用事件的event.preventDefault()方法。 Older browsers (ie 7 and below) dont support this method, so we have to use onclick="return false;" 较早的浏览器(即7及更低版本)不支持此方法,因此我们必须使用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 和html标记应该是

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

Check this question, is it the thing you want? 检查这个问题,这是您想要的东西吗? Disable link using css 使用CSS禁用链接

So that your code will be... 这样您的代码将是...

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

By the way, in HTML the img tag has no end tag. 顺便说一句,在HTML中,img标签没有结束标签。 http://www.w3schools.com/TAGS/tag_img.asp http://www.w3schools.com/TAGS/tag_img.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM