本文介绍了Fancybox麻烦:undefined不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有点麻烦.尝试安装fancybox时出现此javascript错误:

i'm with a little trouble here. Trying to install fancybox i'm getting this javascript error:

'undefined' is not a function (evaluating '$("a.fancybox").fancybox()')

这是我的HTML

<a href="http://localhost/vm/wp-content/gallery/txt/l2.jpg" class="fancybox">
<img src="http://localhost/vm/wp-content/gallery/testando/thumbs/thumbs_l2.jpg">
</a>

和我的JavaScript

and my javascript

<script type="text/javascript">
$(document).ready(function() {
    $("a.fancybox").fancybox();
});
</script>

有人遇到过这个问题吗?

Has anyone encountered this problem?

谢谢!

推荐答案

我假设您使用的是fancybox v1.3.x,因为该版本无法在Fancybox中显示动态添加的元素(FYI Fancybox v2.x可以处理现有的和动态添加的元素).

I am assuming that you are using fancybox v1.3.x because that version cannot display dynamically added elements in Fancybox (FYI Fancybox v2.x can handle existing and dynamically added elements).

如果您不想升级到Fancybox v2.x,则有两个选择:

If you don't want to upgrade to Fancybox v2.x then you have two options:

  1. 使用jQuery livequery插件或
  2. 重建脚本并使用jQuery delegate()触发带有新元素的花式框
  1. Use the jQuery livequery pluginor
  2. Rebuild you script and use jQuery delegate() to trigger fancybox with the new elements

此处中说明了这两个选项.

Both options are explained here.

请注意,使用jQuery delegate()解决方案不适用于Fancybox画廊,仅适用于单个元素.

Please notice that using the jQuery delegate() solution won't work with Fancybox galleries, only with single elements.

如果您不想使用livequery插件,但仍然希望使用动态添加的元素的Fancybox画廊,或者可以升级到jQuery 1.7+并使用新的API jQuery on()

If you don't want to use the livequery plugin and still want to use Fancybox galleries of dynamically added elements, alternatively you could upgrade to jQuery 1.7+ and use the new API jQuery on()

我有一个此处的演示页面,该页面显示了如何使用jQuery on()来显示带有现有元素和动态添加的元素的Fancybox画廊.请随时查看源代码.

I have a demo page here that shows how to use jQuery on() to display Fancybox galleries with existing and dynamically added elements. Please feel free to look at the source code.

这篇关于Fancybox麻烦:undefined不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:51