最佳答案
虽然无法检测到背景图像上的点击,但您可以使用一些聪明的JS和CSS魔法,并在背景图像上设置一个屏蔽元素,如下所示:http://jsfiddle.net/ahmednuaman/75Rxu/,这是代码:
HTML:
CSS:
div
{
display: block;
position: relative;
width: 200px;
height: 200px;
background-repeat: no-repeat;
background-position: center center;
}
#bg_img_1
{
background-image: url('http://placekitten.com/100/100');
}
#bg_img_2
{
background-image: url('http://placekitten.com/100/100');
}
#hit
{
display: block;
position: absolute;
width: 100px;
height: 100px;
background: #000000;
opacity: .3;
margin: 50px;
}
JS:
function handleClick(e)
{
console.log(e.target.id);
}
$( '#bg_img_1' ).click( handleClick );
$( '#hit' ).click( handleClick );
原文链接:https://www.f2er.com/jquery/428017.html