Two differents OnClick on two divs, one over the other

后端 未结 6 1553
心在旅途
心在旅途 2020-12-06 06:50

I have two divs, a big one and a smaller over the other, each div has its own OnClick method. The problem I have is when I clicked the smaller div, the big div\'s OnClick me

6条回答
  •  失恋的感觉
    2020-12-06 07:20

    The best way to detect which element was clicked is to analyze target of event ( click event ). I have prepared small example for this case. You can see it in code below.

    function amIclicked(e, element)
    {
        e = e || event;
        var target = e.target || e.srcElement;
        if(target.id==element.id)
            return true;
        else
            return false;
    }
    function oneClick(event, element)
    {
        if(amIclicked(event, element))
        {
            alert('One is clicked');
        }
    }
    function twoClick(event, element)
    {
        if(amIclicked(event, element))
        {
            alert('Two is clicked');
        }
    }
    

    This javascript method can be called before you execute your script

    Example

    
    
    
    
    
    one
    two

    I hope this helps.

提交回复
热议问题