2012-07-29 50 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

相關問題