[HTML,Script] onClick: на что нажал пользователь?
В каждый див.
SrcElement?
thx
event.cancelBubble=trueНе кроссбраузерно.
stopPropagation еще.
Не кроссбраузерно.
В чем не работает?
Не кроссбраузерно.Да, забыл в первом посте написать - пишется для внутреннего использования, так что можно рассчитывать на IE6/IE7.
ЗЫ: Буду знать, для всех онкликов во вложенных блоках надо в начале писат event.cancelBubble=true
У меня cancelBubble не заработал на каком-то из FF.
У меня cancelBubble не заработал на каком-то из FF.На каком? Просто на том, что у меня стоит - работает.
Turning it off
But usually you want to turn all capturing and bubbling off to keep functions from interfering with each other. Besides, if your document structure is very complex (lots of nested tables and such) you may save system resources by turning off bubbling. The browser has to go through every single ancestor element of the event target to see if it has an event handler. Even if none are found, the search still takes time.
In the Microsoft model you must set the event’s cancelBubble property to true.
window.event.cancelBubble = true
In the W3C model you must call the event’s stopPropagation method.
e.stopPropagation
This stops all propagation of the event in the bubbling phase. Stopping event propagation in the capturing phase is impossible. One wonders why.
For a complete cross-browser experience do
function doSomething(e)
{
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation;
}
Setting the cancelBubble property in browsers that don’t support it doesn’t hurt. The browser shrugs and creates the property. Of course it doesn’t actually cancel the bubbling, but the assignment itself is safe.
http://www.quirksmode.org/
Ага, сенкс.
а в onClick указать параметр функции нельзя?
Какой параметр?
или
onClick="xxx(xxx1)"
Что это тебе даст? -)
Напиши полностью весь код - все эти два вложеннх сля и все события, которые ты на них вешаешь.
<script>
function clicker(x)
{
alert(x);
}
</script>
<div class="outer" id="xxx1" onClick="clicker(1);">xxx1
<div class="inner" id="xxx2" onclick="clicker(2);">xxx2</div></div>
при клике на xxx2 сначала alert(2 затем alert(1)
при клике на xxx1 - только alert(1)
Проблема была именно в том, чтобы при клюике на внутренней области не обрабатывать внешний клик... один из вариантов был - в этом clicker как-то узнать id самого внутрннего блока, в котором произошёл клик, и, если x=1, а этот id - xxx2, то ничего не делать.
В общем, cancelBubble уже помогло.
Оставить комментарий
kruzer25
Как узнать, например, ид самого внутреннего объекта, в котором был сработавший сейчас onClick?В смысле, как для
<div id="xxx1" onClick="xxx">xxx1<div id="xxx2">xxx2</div></div>
получить xxx2, если кликнули на xxx2, и xxx1 в противном случае?