// 点击屏幕收起发布框
$(document).bind('click',function(event){
event.stopPropagation();
bottomNav.animate({bottom : -54 +'px'});
});
editIcon.bind('click',function(e){
var bottomNavBottom = parseInt(bottomNav.css('bottom'));
e.stopPropagation();
// console.log(bottomNavBottom);
if(bottomNavBottom == -54){
bottomNav.animate({bottom : 0 });
return;
}
bottomNav.animate({bottom : -54 +'px'});
});
// 改变文字颜色
commentInput.bind('input',function(e){
e.stopPropagation();
var contentChange = $.trim(commentInput.val());
// bottomNav.css({bottom : 0 });
console.log(contentChange.length)
if(contentChange.length > 0){
commentSpan.css('color','#fff');
}else{
commentSpan.css('color','rgba(255,255,255,0.6)');
}
});
commentSpan.bind('click', function(e) {
e.stopPropagation();
var content = $.trim(commentInput.val());
var newContent = '';
下面的代码省略...
我给document绑定了click事件,点击document的时候可以控制某个元素的bottom值。但是当我点击document的其他位置时确实可以实现,但是当我操作input的时候也会导致bottom的值和document一样。也就是我阻止冒泡失败了。请问是什么原因呢,求解。
你可能没有理解到事件冒泡的含义。
事件冒泡是子元素优先触发,父级元素后触发,最后冒泡到document元素。
如果你直接给document绑定事件,那肯定你点谁都会执行事件,你这里的阻止冒泡并没有什么意义。
假如你给input绑定了一个改变颜色的事件,给document绑定了一个改变bottom的事件。解释如下
因此,如果你想要点击input不执行document的事件,只需要在input绑定事件的时候阻止冒泡就行了