1

我在整个网站使用的主 js 文件中有这个片段

$("a[rel^='prettyPhoto']").prettyPhoto({
        theme: 'light_square',
        showTitle: false,
        allow_resize: true
    });

那个问题是,在某些页面上,prettyPhoto 是未定义的,并导致萤火虫出错,所以我想我会试试这个

if(typeof prettyPhoto=="undefined"){
    //do nothing
}else{
    $("a[rel^='prettyPhoto']").prettyPhoto({
        theme: 'light_square',
        showTitle: false,
        allow_resize: true
    });
}

但这始终执行为 true,即使在 prettyPhoto 可用的页面上也是如此......任何想法

4

2 回答 2

9

尝试这个:

if (typeof $.fn.prettyPhoto == "function") {
    // we have prettyPhone on the page
}
于 2011-10-12T20:48:07.427 回答
0

如果你在一个你知道有 prettyPhoto 的页面上执行 console.log(prettyPhoto),它说的是什么?一个东西?

如果是这样,那么你做

if(typeof prettyPhoto === 'object'){
    //do your stuff here.
}
于 2011-10-12T20:48:28.980 回答