[英]jQuery ~ Two click events overlayed?
我有一個容器元素說
<div id="myContainer">
<form action="" method="post">
Radio: <input type="radio" name="myRadio" id="myRadio" value="1" />
</form>
</div>
因此,如果我將$('#myContainer')
click事件分配給$('#myContainer')
並將$('#myRadio')
click事件分配給$('#myRadio')
是否有辦法使它們同時啟動? 當前,因為在表單元素“上方”有一個單擊事件,該事件具有return false
以停止頁面跳轉,所以我無法單擊單選按鈕。
有辦法解決嗎? 還是我需要在選擇器中更具體? 甚至$('#myContainer:not("#myRadio")')
嗎? (甚至有效嗎?)
要停止頁面跳轉,請使用event.preventDefault()
而不是return false;
當前正在停止該事件,使其無法正常運行。
自然的行為是使事件冒泡 ,因此要使兩個處理程序都觸發,您要做的所有事情都不會通過return false
:)干擾冒泡:)
您可以執行以下操作!
$('#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.