【问题标题】:Lightbox close within body灯箱关闭在体内
【发布时间】:2016-09-27 16:47:58
【问题描述】:

我的客户要求在点击页面中的任何位置时打开灯箱。所以我创建了一个函数并使用onclick 事件在正文中调用它。

Javascript:

function openLighbox(){
  $('a.html5lightbox').trigger('click');
}

HTML:

<body onclick="openLighbox()">
  <a class="html5lightbox" href="form.html"><img src="cmDest.jpg"></a>
</body>

通过上面的代码,我已经成功打开了灯箱,但是当我尝试关闭灯箱时,它无法关闭,因为点击再次打开它。

我的问题是如何防止它再次打开?

【问题讨论】:

  • 关闭灯箱后,再次点击页面会怎样?

标签: javascript jquery html lightbox


【解决方案1】:

仅当元素不是灯箱时才触发点击,移除onclick属性

$('body').click(function(e){
 console.log(e.target)
if ( !$(e.target).closest('#html5box-html5-lightbox').length) {

                    $('a.html5lightbox').trigger('click');
        } 
});

https://jsfiddle.net/oxtr7me0/

【讨论】:

  • 当我点击关闭按钮时。再次打开灯箱。
  • 能否提供灯箱文档的链接?
  • 还是一样的情况
  • 你能提供一个jsfiddle吗?
  • 我要去吃午饭了。感谢您花时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多