Disable the onclick event of a parent element

后端 未结 5 1980
清酒与你
清酒与你 2021-01-07 05:13

Let\'s say I have an element with an onclick event, and another element inside it, how do I disable the onclick for the child element?

Example:

5条回答
  •  青春惊慌失措
    2021-01-07 06:04

    Javascript events are propagated bottom-up, i.e if you click on a button, all ascendants recieve the onclick event in orderly fashion, except for when one of them intercepts it via stopPropagation (or returns true, meaning i handled this)

    Example :

    text

    you could also bind onclick of parent to a function :

    function onlick_handler(e)
    {
       e.stopPropagation(); //this event would propagate to parents of this object which are clicked as well
       // the rest here
    }
    

提交回复
热议问题