Disable the onclick event of a parent element

后端 未结 5 1970
清酒与你
清酒与你 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条回答
  •  萌比男神i
    2021-01-07 05:57

    Your handler (the "do something"-part of the h3-tag) can check the tagName of the originating element:

    var origin = event.srcElement || event.target;
    if (origin.tagName && origin.tagName.match(/h3/i)){
      // do things
    } else {return true;}
    

    Now for the checkbox the click handler is triggered, but nothing will happen. Alternatively, you could supply the h3-tag with a unique id an check the id of the clicked element.

提交回复
热议问题