禁用父元素的onclick事件 [英] Disable the onclick event of a parent element

查看:43
本文介绍了禁用父元素的onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有onclick事件的元素,并且其中有另一个元素,如何禁用子元素的onclick?

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?

示例:
< h3 onclick =做某事"><输入type ="checkbox" name ="child"/>文字</h3>

选中该复选框时,不应触发onclick事件.

When you check the checkbox, the onclick event shouldn't be triggered.

谢谢.

推荐答案

您的处理程序(h3-tag的"执行某事"部分)可以检查 tagName 原始元素:

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;}

现在,单击处理程序 的复选框已触发,但是什么也不会发生.另外,您可以为h3-tag提供唯一ID,然后检查被点击元素的ID.

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.

这篇关于禁用父元素的onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆