如何检查页面上是否存在fancybox

che*_*ell 8 jquery fancybox

我正在尝试检查是否已经加载了fancybox.如果是这样,那么我运行一个ajax脚本将内容放入其中.

如果没有,我打开一个fancybox iframe,它取出内容.

这是代码:

if ($('div#fancybox-frame:empty').length >0) { 
    alert('it is empty');

$.fancybox({
   'width': '80%',
    'height': '80%',
    'autoScale': true,
    'transitionIn': 'fade',
    'transitionOut': 'fade',   
    'type': 'iframe', 
    'href': '/show_photo'
});
 }
 else{
    alert('it is full');   
    $.ajax({
        type: "GET",
        url: "/show_photo",
        success: function(data){
        cropping_arrived();
        }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

即使我知道没有打开fancybox,它总是会返回它已满.

有任何想法吗?

小智 36

我使用下面的代码:

if(typeof $.fancybox == 'function') {
     fancy box loaded;
} else {
     fancy box not loaded;
}
Run Code Online (Sandbox Code Playgroud)


Jas*_*aro 1

这应该可以工作,无需测试.length

if ($('div#fancybox-frame:empty'))
Run Code Online (Sandbox Code Playgroud)

但是,:empty会检查文本节点。所以如果你div有一个换行符,即

<div id="#fancybox-frame">
</div>
Run Code Online (Sandbox Code Playgroud)

它可能会窒息。在这种情况下,请将开始标签和结束标签放在同一行。