Javascript 未捕获的类型错误:对象 [object Object] 没有方法“fancybox”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9070979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Object [object Object] has no method "fancybox"
提问by mbajur
I have really strange problem with fancybox(2.x). I was using this script for ages but i hadn't experienced such problem before.
我对fancybox(2.x)有很奇怪的问题。我使用这个脚本很长时间了,但我以前从未遇到过这样的问题。
So, i have website: http://tinyurl.com/6mx7an8and i've included every jquery/fancybox files as manual says but the JS console gives me the error
所以,我有网站:http: //tinyurl.com/6mx7an8并且我已经按照手册包含了每个 jquery/fancybox 文件,但是 JS 控制台给了我错误
Uncaught TypeError: Object [object Object] has no method 'fancybox'
Uncaught TypeError: Object [object Object] has no method 'fancybox'
Have you any idea what am i doing wrong? Any help would be appreciated :)
你知道我做错了什么吗?任何帮助,将不胜感激 :)
回答by ShankarSangoli
There are couple of issues in your website.
您的网站有几个问题。
- You are including jQuery library 2 times one before and after including fancybox plugin js because of which the plugin which is added is cleared by second jQuery library inclusion.
- There is some other library which is overridding
$
, so your code is not working because$
is not an alias forjQuery
anymore. You can usejQuery.noConflict()
to avoid conflicts with other libraries on the page which use the same variable$
.
- 您在包含fancybox插件js之前和之后包含2次jQuery库,因此添加的插件被第二个jQuery库包含清除。
- 还有一些其他库被覆盖
$
,因此您的代码无法正常工作,因为$
它不再是别名jQuery
。您可以使用jQuery.noConflict()
以避免与页面上使用相同变量的其他库发生冲突$
。
回答by danwellman
Looks like jQuery is included twice, once near the top of the head and once near the bottom.
看起来 jQuery 包含两次,一次靠近头顶,一次靠近底部。
回答by Brad Fellows
I had a similar problem. Somehow, jQuery was being loaded twice. Turns out while testing something, I added a drupal_add_js line to load jquery in a block, which was disabled, but Drupal still evaluated the PHP in the disabled block.
我有一个类似的问题。不知何故,jQuery 被加载了两次。结果在测试某些东西时,我添加了一条 drupal_add_js 行来在一个块中加载 jquery,该块被禁用,但 Drupal 仍然评估了禁用块中的 PHP。
When I removed the drupal_add_js line from the block, jQuery loaded fine, once per page.
当我从块中删除 drupal_add_js 行时,jQuery 加载正常,每页一次。
回答by Alexandru Trandafir Catalin
I have a css unify php script on my website that added both jquery and jquery postmessage plugin into the same javascript file.
我的网站上有一个 css unify php 脚本,它将 jquery 和 jquery postmessage 插件添加到同一个 javascript 文件中。
So it was giving me the error above.
所以它给了我上面的错误。
I have loaded the jquery postmessage separately without unifying it and it stopped giving me the error.
我已经单独加载了 jquery postmessage 没有统一它,它停止给我错误。
回答by Joseph N.
I got it working with this code
我用这个代码让它工作
<script type="text/javascript">
(function( $ ) {
$(document).ready( function( ) {
$("a#fancyBoxLink").fancybox({
'href' : '#popupvid',
'titleShow' : false,
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
});
});
})(jQuery);
</script>
回答by Vinayak Shedgeri
Jquery sub library may included in main library ,For example jquery.ui.sortable.js i have included in jquery-ui.js while downloading and i added again it was giving a error for me.
jquery 子库可能包含在主库中,例如 jquery.ui.sortable.js 我在下载时包含在 jquery-ui.js 中,我再次添加它给我一个错误。
script type="text/javascript" src="vendor/js/jquery-ui.js">
脚本类型="text/javascript" src="vendor/js/jquery-ui.js">
script type="text/javascript" src="vendor/js/jquery.ui.sortable.js">
脚本类型="text/javascript" src="vendor/js/jquery.ui.sortable.js">
so verify sub library included or not
所以验证子库是否包含