admin管理员组文章数量:1022516
I've got a div and an image overlapping one over the other. The image is in front of the div. Is it possible to be able to click on the div ignoring the image?
<img src="smiley.gif" style="z-index: 2; width:200px; height 200px;">
<div id="theDiv" style="background-color: #f60; width:200px; height:200px;"></div>
$('#theDiv').click(function(){
alert("you've clicked on "+this.id);
});
I've got a div and an image overlapping one over the other. The image is in front of the div. Is it possible to be able to click on the div ignoring the image?
<img src="smiley.gif" style="z-index: 2; width:200px; height 200px;">
<div id="theDiv" style="background-color: #f60; width:200px; height:200px;"></div>
$('#theDiv').click(function(){
alert("you've clicked on "+this.id);
});
Share
Improve this question
edited Aug 25, 2015 at 17:56
Becky
asked Aug 25, 2015 at 17:44
BeckyBecky
5,6159 gold badges44 silver badges79 bronze badges
3
- But it's really necessary to click beneath the image? Why will you use it? – gioNicol Commented Aug 25, 2015 at 18:00
- Does the image have to be in front of the div? The div would have to be on top, using z-index with a position noted in the css. – Georgette Pincin Commented Aug 25, 2015 at 18:00
- 1 Yeah, something's odd about your setup. What is the purpose of the smiley? Would it be better to just make the smiley a background image of the div or to put it inside the div? You can always use jQuery next() or closest() to acplish this, but I have a feeling that there's something off about the markup setup. – Gene Parcellano Commented Aug 25, 2015 at 18:03
2 Answers
Reset to default 7You have to use CSS pointer-events
property. Just add to img styles:
pointer-events: none;
EDIT: I'm sorry, I made a mistake, it has very good support! Can I use
Assign an id to your image:
<img id='image' src="smiley.gif" style="z-index: 2; width:200px; height 200px;">
and use click event on image to do whatever you want to do with your div
$('#image').click(function(){
//perform tasks on your div here
$('#theDiv').css('background-color', '#fff');
});
I've got a div and an image overlapping one over the other. The image is in front of the div. Is it possible to be able to click on the div ignoring the image?
<img src="smiley.gif" style="z-index: 2; width:200px; height 200px;">
<div id="theDiv" style="background-color: #f60; width:200px; height:200px;"></div>
$('#theDiv').click(function(){
alert("you've clicked on "+this.id);
});
I've got a div and an image overlapping one over the other. The image is in front of the div. Is it possible to be able to click on the div ignoring the image?
<img src="smiley.gif" style="z-index: 2; width:200px; height 200px;">
<div id="theDiv" style="background-color: #f60; width:200px; height:200px;"></div>
$('#theDiv').click(function(){
alert("you've clicked on "+this.id);
});
Share
Improve this question
edited Aug 25, 2015 at 17:56
Becky
asked Aug 25, 2015 at 17:44
BeckyBecky
5,6159 gold badges44 silver badges79 bronze badges
3
- But it's really necessary to click beneath the image? Why will you use it? – gioNicol Commented Aug 25, 2015 at 18:00
- Does the image have to be in front of the div? The div would have to be on top, using z-index with a position noted in the css. – Georgette Pincin Commented Aug 25, 2015 at 18:00
- 1 Yeah, something's odd about your setup. What is the purpose of the smiley? Would it be better to just make the smiley a background image of the div or to put it inside the div? You can always use jQuery next() or closest() to acplish this, but I have a feeling that there's something off about the markup setup. – Gene Parcellano Commented Aug 25, 2015 at 18:03
2 Answers
Reset to default 7You have to use CSS pointer-events
property. Just add to img styles:
pointer-events: none;
EDIT: I'm sorry, I made a mistake, it has very good support! Can I use
Assign an id to your image:
<img id='image' src="smiley.gif" style="z-index: 2; width:200px; height 200px;">
and use click event on image to do whatever you want to do with your div
$('#image').click(function(){
//perform tasks on your div here
$('#theDiv').css('background-color', '#fff');
});
本文标签: javascriptClick on a lower zindex elementStack Overflow
版权声明:本文标题:javascript - Click on a lower z-index element - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745491481a2152982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论