event.stopPropagation Not Working in Firefox

后端 未结 2 1752

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.

提交回复
热议问题