admin管理员组

文章数量:1287598

I have a two-level ul/li menu where the li can fire of 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 have a two-level ul/li menu where the li can fire of 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

Share Improve this question asked Nov 12, 2010 at 12:00 TesonTeson 6,7369 gold badges50 silver badges70 bronze badges 1
  • are you using straight javascript or a js library to set events? – fcalderan Commented Nov 12, 2010 at 12:04
Add a ment  | 

2 Answers 2

Reset to default 10

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./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.

本文标签: javascriptavoid double events (onclick) with nested ulliStack Overflow