在这个例子中如何指定.stopPropagation()?
我使用以下代码来获得“单击外部”以关闭工作:
单击 div 时显示:
$('div.topIconNew').click(
function(){
$(this).siblings('div.topTip').show();
$(this).siblings('div.topDrop').slideDown(240);
单击 div 外部隐藏时:
$('div#wrapper').click(
function(event){
$('div.topIconNew, div.topTip, div.topDrop').hide();
}
);
$('div.topTip, div.topDrop').click(
function(event){
event.stopPropagation();
}
);
上面的问题是 topIconNew .stopPropagation 使得一旦 topTip 和 topDrop div已打开,如果我再次单击相同的 topIconNew (即切换)或 topIconNew 的另一个实例,它们将不会关闭。
新编辑***
好吧,我正在思考这个问题,想知道解决方案是否可以使用 if 语句。
所以我最终想要以下附加代码:
$('topIconNew').click(
function(){
$('div.topTip, div.topDrop').hide();
}
);
但是,我希望上面的内容也说仅在单击topIconNew时隐藏topTip和topDrop如果仅打开topTip和topDrop并且隐藏当前打开的 topTip 和 topDiv 元素。
上面的话我该怎么说呢?
I have used the following code to get "click outside" to close working:
On click the divs show:
$('div.topIconNew').click(
function(){
$(this).siblings('div.topTip').show();
$(this).siblings('div.topDrop').slideDown(240);
On click outside the divs hide:
$('div#wrapper').click(
function(event){
$('div.topIconNew, div.topTip, div.topDrop').hide();
}
);
$('div.topTip, div.topDrop').click(
function(event){
event.stopPropagation();
}
);
The problem with that above is that the topIconNew .stopPropagation makes it so that once the topTip and topDrop divs are open, they won't close if I click that same topIconNew again (i.e. toggle) or another instance of topIconNew.
NEW EDIT***
Okay, I'm thinking through this and wondering if the solution might be with if statements.
So I ultimately want the following additional code:
$('topIconNew').click(
function(){
$('div.topTip, div.topDrop').hide();
}
);
However, I want the above to also say to hide topTip and topDrop upon clicking topIconNew only if the topTip and topDrop are open AND only hide the topTip and topDiv elements that are currently open.
How would I say the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并没有直接回答你的问题,但我刚刚看到有人以一种非常优雅的方式实现了“单击任意位置关闭弹出窗口”方法;需要注意的是,页面的其余部分必须谨慎使用
e.stopPropagation()
。This doesn't answer your question directly, but I just saw someone on SO implement the 'click anywhere to close pop-up' method in a pretty elegant way; the caveat is that the rest of your page must use
e.stopPropagation()
sparingly.