我試圖切換.abb-bar
,使用$.toggle()
,只要其父(.abb
)得到一個點擊。但每當.abb-bar
得到一個點擊,它會自己切換,因爲event-bubbling
。 e.stopPropagation()
應該解決問題,但它不起作用。可以做些什麼?stopPropagation()不適用於委託事件
HTML
<span class="col-auto entry-ops abb">
<i class="fa fa-bars"></i>
<div class="list-group abb-bar">
<button class="list-group-item list-group-item-action d-flex justify-content-center" data-user="nick">follow</button>
<button class="list-group-item list-group-item-action d-flex justify-content-center complain">complain</button>
<button class="list-group-item list-group-item-action d-flex justify-content-center show-entry-number" data-toggle="tooltip">'.$row['entry_id'].'</button>
</div>
</span>
JS
$(document).on('click', '.abb', function(e) {
e.stopPropagation();
$(this).children('.abb-bar').toggle();
});
stopPropagation將停止從點火那是它的父母......不是孩子的事件......但你坐在文件,所以點擊已經到達那裏。 – epascarello
我沒有得到它爲什麼人們downvote別人的答案,不給理由 – krishnar
@ krishnar你的答案可能是downvoted,因爲答案只包括代碼和解釋文本。嘗試添加一些關於您的代碼更改內容的解釋,以創建更好的收到答案。 –