avoid double events (onclick) with nested ul / li
I have a two-level ul/li menu where the li can fire o开发者_Go百科f different onclick-events.
It seems as the parent-event (ul li), is fired when clicking one ul li ul li-item.
Can I avoid this an easy way.
(I'm considering using timer to trap second event, but think of it as an ugly fix...)
Regards, /T
I suppose you have to stop the event bubbling
so if you are using straight javascript you have event.cancelBubble = true
otherwise if you're using jQuery you have event.stopPropagation()
or event.stopImmediatePropagation()
http://api.jquery.com/category/events/event-object/
Instead of event.stopPropagation()
consider:
function myFunction() {
if (event.myFunctionResolved == undefined) {
event.myFunctionResolved = true;
// your logic here
}
}
The advantage here is that the event won't be stopped and hence other scripts that might be listening for that event will be executed.
精彩评论