开发者

avoid double events (onclick) with nested ul / li

开发者 https://www.devze.com 2023-01-24 13:42 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消