我需要在点击页面(# page )的背景时触发一个事件(例如隐藏一个浮动的购物车),但当点击内容内部时不会发生这个事件。所以我需要这个事件发生在空间:页面减去内容。我该如何实现它?谢谢
如果我有这样的结构:
<body>
<div id="page">
<div id="content">
here
</div>
</div>
</body>
var outer= jQuery("#page");
jQuery(outer).click(function(){
jQuery(cos_de_cumparare).toggle();
});发布于 2012-02-03 18:26:02
您可以检查事件的目标并相应地执行操作
var outer= jQuery("#page");
outer.click(function(e){
//trigger the event only if the target of the click is the page
if(e.target.id === 'page'){
alert('click');
}
});在这里拉小提琴http://jsfiddle.net/wNsd7/
发布于 2012-02-03 18:27:19
要做到这一点,您可以停止事件在链中向上传播。
通常的方法是将单击事件附加到子元素,并从那里停止传播,这样:
$("#page div").click(function(e){
e.stopPropagation();
})发布于 2012-02-03 18:41:31
试试这个,然后检查http://jsfiddle.net/qgUjb/4/
$("#page").click(function(event) {
if(event.target.id == "content") return;
$("#content").toggle();
});https://stackoverflow.com/questions/9127040
复制相似问题