<html>
<head>
<style type="text/css">
#shovel
{
background-image:url("Images/Custom/ShovelBank.png");
background-repeat:no-repeat;
}
img
{
cursor:pointer;
}
</style>
<script type="text/javascript">
function abc()
{
var e = event.srcElement;
alert(e.id);
alert(e.src);
}
</script>
</head>
<body>
<div id="shovel" onclick="abc()">
<img src="Images/Custom/Shovel.png"></img>
</div>
</body>
</html>
需要说明一下,div的背景图片和img的图片大小是一样的;
问题在于:每次我点击这个div层的时候,
e.id输出是空,反倒是e.src是img的src,
也就是说event.srcElement是img,
这样小弟就很不解了,我明明是在div上挂载的onclick事件,
结果却是img触发的,彻底晕了,望高手指点!
------解决方案--------------------
楼主 看似是点的div 但实际是点的img
- HTML code
<html> <head> <style type="text/css"> #shovel { background-image:url("Images/Custom/ShovelBank.png"); background-repeat:no-repeat; } img { cursor:pointer; } </style> <script type="text/javascript"> function abc(ee) { var e = ee.srcElement || ee.target; alert(e.id); alert(e.src); } </script> </head> <body> <div id="shovel" onclick="abc(event)">123 <img src="Images/Custom/Shovel.png"></img> </div> </body> </html>