简体   繁体   English

绝对定位的img,jquery对click事件没有反应

[英]absolute positioned img, jquery not reacting on click event

I'm making images gallery and I want to show delete icon on image. 我正在制作图片库,我想在图片上显示删除图标。 I made div containter for image and icon with relative position and icon img with position absolute. 我做了div容器的图像和图标的相对位置和图标img的绝对位置。 But jquery .click and .on('click',function(){...}); 但jQuery的.click.on('click',function(){...}); not working. 不工作。 Here is my code (I'm using blueimp-gallery): 这是我的代码(我正在使用blueimp-gallery):

HTML: HTML:

<div class="image">
    <img class="delete" src="/img/delete1.png" value="68">
    <a href="/images/018ce3de8.jpg" title="" data-gallery="">
        <img src="/images/thumbs/018ce3de8.jpg" alt="">
    </a>
</div>

CSS: CSS:

.image {
    width: 250px;
    height: 250px;
    display: inline-block;
    position: relative;
}

.delete {
    height: 16px!important;
    width: 16px!important;
    top: 10px;
    left: 224px;
    position: absolute;
}

JQuery: jQuery的:

$('.delete').click( function() {
    alert( 'click' );
});

When .delete is set changed another class it works. 设置.delete ,更改另一个类即可。 Why it's not working with this absolute positioned div? 为什么此绝对定位的div不起作用? When I hover .delete div mouse icon is changing so it's on top. 当我将鼠标悬停在.delete div鼠标图标时,它就在顶部。

You have to wait for the DOM to fully load before you attach the event handler. 在附加事件处理程序之前,您必须等待DOM完全加载。 Change your javascript like below 如下更改您的JavaScript

$(document).ready(function() {
    $('.delete').click(function() {
        alert('clicked');
    });
});

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

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