这是我的代码:
<div id="a" style="position:absolute;width:200px;height:200px;background:red;word-wrap:break-word;">
<div id="b" style="width:50px;height:50px;background:blue;"></div>
</div>
脚本是:
$( "#b" ).draggable({ containment: 'parent' });
$('#a').click(function(e){
alert(e.pageX)
//return false;
})
当我点击红色div时,我想提醒,而不是蓝色div,
演示在这里:http://jsfiddle.net/KwYjr/2/
感谢
答案 0 :(得分:4)
测试e.target
的ID。
示例: http://jsfiddle.net/patrick_dw/KwYjr/6/
$('#a').click(function(e){
if( e.target.id === 'a' ) {
alert(e.pageX);
}
//return false;
});
另一种选择是向调用#b
的{{1}}元素添加点击处理程序,但我不推荐它。有更好的方法(如上所述),而不是为了停止传播而分配处理程序。
编辑:
运行上述测试的另一种方法是直接比较元素,而不是使用其ID:
示例: http://jsfiddle.net/patrick_dw/KwYjr/8/
e.stopPropagation()
答案 1 :(得分:3)
如果你像这样使用event.stopPropagation()
,那就有效:
$( "#b" ).draggable({ containment: 'parent' }).click(function(e){ e.stopPropagation(); });
$('#a').click(function(e){
alert(e.pageX);
})
答案 2 :(得分:0)
使用
event.preventDefault()
http://api.jquery.com/event.preventDefault/
答案 3 :(得分:0)
e.preventDefault();
e.stopPropagate();