event.stopPropagation Not Working in Firefox

后端 未结 2 1750

I am building a sidebar vertical menu that contains Main Menu items and one level of sub-menu items.

I am using Javascript and CSS so that when the user clicks a top-lev

相关标签:
2条回答
  • 2021-01-24 06:50

    Javascript now..

    inline javascript is not a proper way to write code

    This example shows a 'innovative' solution to handle a menu.

    it also shows you how to correctly handle the event and the event's target.

    It uses:

    1. classList
    2. dataset
    3. one handler for multiple elements.
    4. css to display and hide thee elements.

    function handler(e){
     e=e||window.event;
     var target=e.target||e.srcElement;
     var action=target.dataset['action']||'no action';
     !(action=='toggle')||(target.childNodes[1].classList.toggle('show'));
     !target.dataset['url']||alert(target.dataset['url']);
    }
    var firstUL=document.getElementsByTagName('ul')[0];
    firstUL.addEventListener('click',handler,false);
    

    DEMO

    http://jsfiddle.net/Jj6FY/1/

    the boring stuff is that you need to find the various elements with getEl...

    but at the end you have more control over everything.

    and here is a accordion function.

    http://jsfiddle.net/YjCbM/1/

    if you have any questions just ask.

    0 讨论(0)
  • 2021-01-24 06:54

    You need to pass the event as an argument to your function.

    goTo(event, '#')
    

    Then you can do event.stopPropagation() and it knows what event is.

    http://jsbin.com/OyUvUqa/2

    0 讨论(0)
提交回复
热议问题