jQuery stopPropagation无法正常工作 [英] jQuery stopPropagation not working
问题描述
我正在使用以下颜色选择器,它工作正常,但是当我单击颜色选择器图标时,我不希望它冒泡到身体.因此,我尝试了以下操作,但现在颜色选择器无法正常工作.
I'm using the following color picker and it works fine, but when i click on the color picker icon, i don't want it to bubble to body. So i tried the following and now the color picker is not working.
检查 http://jsfiddle.net/CWGgM/ .
Check http://jsfiddle.net/CWGgM/.
如果您从jsfiddle中删除以下代码,则它可以工作.是什么原因造成的
If you remove the below code from jsfiddle then it works. What is causing this
$('#test').click(function(e){
e.stopPropagation();
});
推荐答案
It appears to use its own live()
style code, where events are allowed to bubble up and are handled on the document
.
因此,事件必须传播到document
否则将无法正常工作.
Therefore, the events must propagate to document
or they won't work.
您可以通过以下解决方法避免事件在身体上触发...
You could avoid the event triggering on body with the following workaround...
$('body').click(function(event) {
if ($(event.target).parent()[0] == $('.mColorPickerTrigger')[0]) {
return true;
}
});
jsFiddle .
或者这可能与多个颜色选择器一起使用会更好...
Or this might work better with multiple color pickers...
$('body').click(function(event) {
if ($(event.target).parent().hasClass('mColorPickerTrigger')) {
return true;
}
});
jsFiddle .
这篇关于jQuery stopPropagation无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!