2012-07-29 47 views
2

如何使这项工作:如何使鼠标事件仅适用于外部DIV?

<div onclick="alert('You clicked me!')" style="background:lightblue;display:inline-block;padding:20px"> 
    <textarea rows="5" cols="50">You can click and select text here normally, but I don't register mouse events from the DIV behind me!</textarea> 
    <div id="inner" style="background:lightgreen">Same here. I want my own mouse events - not the ones from #inner.</div> 
</div> 

谢谢!

+1

您需要检查事件的来源。 – nhahtdh 2012-07-29 18:33:00

+0

你如何检查事件的来源?这可以内联完成吗(即使不推荐)?有没有办法简单地告诉一个元素不是来自其父项的固有事件? – user353885 2012-07-29 18:48:35

+0

TEXTAREA不会在DIV后面注册鼠标事件?那是什么意思? #inner DIV需要自己的鼠标事件 - 不是来自#inner的事件?那是什么意思?在你的问题中提供更多信息。 – 2012-07-29 18:48:50

回答

3

我猜你想这样的:

<div onclick="if (event.target === this) alert('You clicked me!')"> 

现场演示:http://jsfiddle.net/UZtag/1/

因此,警报不弹出如果单击文本区域或#inner DIV,但只有直接点击外部DIV。

+0

太棒了,谢谢! – user353885 2012-07-29 20:41:17

相关问题