[英]jQuery ~ Two click events overlayed?
I have a container element say 我有一个容器元素说
<div id="myContainer">
<form action="" method="post">
Radio: <input type="radio" name="myRadio" id="myRadio" value="1" />
</form>
</div>
So if I assign a click event to the $('#myContainer')
and also a click event to $('#myRadio')
is there a way to get them both to fire? 因此,如果我将
$('#myContainer')
click事件分配给$('#myContainer')
并将$('#myRadio')
click事件分配给$('#myRadio')
是否有办法使它们同时启动? Currently, because there is a click event "above" the form element, which has a return false
to stop the page jumping, I cannot click the radio button. 当前,因为在表单元素“上方”有一个单击事件,该事件具有
return false
以停止页面跳转,所以我无法单击单选按钮。
Is there a way to tackle this? 有办法解决吗? Or do I need to be more specific in my selectors?
还是我需要在选择器中更具体? Or even
$('#myContainer:not("#myRadio")')
? 甚至
$('#myContainer:not("#myRadio")')
吗? (is that even valid?) (甚至有效吗?)
To stop the page jumping use event.preventDefault()
instead of return false;
要停止页面跳转,请使用
event.preventDefault()
而不是return false;
which is currently stopping the event dead in its tracks. 当前正在停止该事件,使其无法正常运行。
The natural behavior is for the event to bubble , so all you need to do to get both handlers firing is not interfering with the bubble via return false
:) 自然的行为是使事件冒泡 ,因此要使两个处理程序都触发,您要做的所有事情都不会通过
return false
:)干扰冒泡:)
You can do the following! 您可以执行以下操作!
$('#myContainer, #myRadio').click(function(event){
event.stopPropagation(); //This will stop the bubble so it only fires once per element
switch(event.srcElement)
{
case 'HTMLFormElement':
//The Div / Form
break;
case 'HTMLInputElement':
//The input itself
break;
}
return false;
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.