我無法縫停止點擊信號傳播到父元素。我在這裏搜索並閱讀了很多問題,但找不到可行的解決方案。我正在Firefox中測試。我似乎無法停止單擊事件傳播到父元素
//this is simply a div element
$("#" + self.id).css({
"position" : "absolute"
});
$("#" + self.id).append(
'<div class="onoffswitch" id="'+ self.id + "_checkbox_d" + '">'+
'<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="'+ self.id + "_checkbox" +
(self.isOn ? '" checked>' : '">') +
'<label class="onoffswitch-label" for="'+ self.id + "_checkbox" + '">'+
'<div class="onoffswitch-inner"></div>'+
'<div id="switch-mod' + self.id + '" class="onoffswitch-switch onoffswitch-switchon"></div>'+
'</label>'+
'</div>');
$("#" + self.id + "_checkbox_d").css({
"position" : "absolute"
});
$('#' + self.id + "_checkbox").click(function(event){
if ($(this).is(':checked')) {
$("#switch-mod" + self.id ).removeClass("onoffswitch-switchoff");
$("#switch-mod" + self.id ).addClass( "onoffswitch-switchon");
}
else{
$("#switch-mod" + self.id ).removeClass("onoffswitch-switchon");
$("#switch-mod" + self.id ).addClass( "onoffswitch-switchoff");
}
event.stopPropagation();
event.stopImmediatePropagation();
});
$("#" + self.id).click(function() {
$('html').trigger("selection-panel-clicked", self.id);
});
父元素的最後一個點擊處理程序被調用,我需要防止。
編輯1:
我認爲這個問題必須與CSS的複選框/撥動開關。我從這裏自動生成它,http://proto.io/freebies/onoff/。
我發現首先調用父級的點擊處理程序,然後調用子級,然後再次調用父級。停止訴訟不會改變這一點。返回錯誤的結果在父母然後孩子,但這也導致切換開關不工作。
開關包括CSS這些特性,
.onoffswitch-switch {
-moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
-o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s;
}
.onoffswitch-inner {
-moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
-o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}
也許這是相關的?
編輯2:我最終解決了我的問題,將盒子拿出父母,並將它們放在父母父母的父母身上,父母不響應點擊,然後根據他們對應的面板位置進行定位至。我仍然想知道爲什麼我有這個問題。
當我試了一下(當然,以一種不可思議的方式,但仍然),你的代碼對我的作品在Chrome中。你是否可以簡化代碼並添加HTML(有一個元素,動態添加元素和事件並查看它們是否仍然傳播)? – PhistucK
奇怪的是,它接縫父元素處理子元素之前的點擊信號。 – tAllan