如何使鼠标事件仅适用于外部DIV?

时间:2012-07-29 18:31:26

标签: javascript mouseevent

我如何做这项工作:

<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 个答案:

答案 0 :(得分:3)

我猜你想要这个:

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

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

因此,如果单击TEXTAREA或#inner DIV,则警报会弹出,但仅在直接单击外部DIV时才会弹出。