js阻止冒泡行为:
if(e.stopPropagation) { //W3C阻止冒泡方法 e.stopPropagation(); } else { e.cancelBubble = true; //IE阻止冒泡方法 }
JQuery 提供了两种方式来阻止事件冒泡。
方式一:
event.stopPropagation(); $("#div1").mousedown(function(event){ event.stopPropagation(); });
方式二:
return false; $("#div1").mousedown(function(event){ return false; });
但是这两种方式是有区别的。return false 不仅阻止了事件往上冒泡,而且阻止了事件本身。event.stopPropagation() 则只阻止事件往上冒泡,不阻止事件本身。
$(document).mouseup(function(e){ var _con = $(' 目标区域 '); // 设置目标区域 if(!_con.is(e.target) && _con.has(e.target).length === 0){ // Mark 1 some code... // 功能代码 } });