【问题标题】:jQuery is not defined and Object [object Object] has no method 'fancybox' in OSClassjQuery 未定义且 Object [object Object] 在 OSClass 中没有方法 'fancybox'
【发布时间】:2013-03-01 12:20:15
【问题描述】:

我正在使用 OSClass。当转到http://localhost/osclass/index.php?page=item&id=2 页面时,会出现以下错误。

Uncaught ReferenceError: jQuery is not defined 
(anonymous function) 

Uncaught TypeError: Object [object Object] has no method 'fancybox' 
(anonymous function)
o 
p.fireWith 
e.extend.ready 
c.addEventListener.B 

Uncaught TypeError: Cannot call method 'create' of undefined
(anonymous function)

在那个文件中我的 js 是,

osc_register_script('fancybox', osc_current_web_theme_js_url('fancybox/jquery.fancybox.js'));

osc_enqueue_script('fancybox');
osc_enqueue_script('jquery-validate');

osc_enqueue_style('fancybox', osc_current_web_theme_js_url('fancybox/jquery.fancybox.css'));

    <script type="text/javascript">
        $(document).ready(function(){
            $("a[rel=image_group]").fancybox({
                openEffect : 'none',
                closeEffect : 'none',
                nextEffect : 'fade',
                prevEffect : 'fade',
                loop : false,
                helpers : {
                        title : {
                                type : 'inside'
                        }
                }
            });
        });
    </script>

我的问题在哪里?

【问题讨论】:

  • 你能把你正在使用的javascript贴在你的页面上吗?

标签: jquery osclass


【解决方案1】:

我今天早上修复了它,这是提交:https://github.com/osclass/Osclass/commit/cd9f75080f42d681be2e6bce709269073c0bcec2

问题是fancybox的依赖没有设置

【讨论】:

  • 这是在您之前 4 小时给出的以下答案的一个不太详细的版本。不知道它是如何被接受的,没有冒犯。
  • 因为 JQuery 已经被注册/入队(在 oc-load.php 文件中),问题是fancybox 没有设置依赖关系,它是在jquery 之前加载的。正确的解决方法是设置依赖项(所以 fancybox 将在 jQuery 之后加载)。
【解决方案2】:

当抛出错误的脚本尝试使用它时,不会加载 jQuery。第一个错误都源于 jQuery 未定义。

jQuery 需要在 anything 之前加载,否则可能依赖它。

由于您使用这些函数来加载您需要放置的东西,因此在任何其他可能需要 jQuery 的 javascript 之前:

osc_register_script('jquery', osc_assets_url('path/to/jquery.js'));
osc_enqueue_script('jquery');

通过查看与您正在使用的内容相关的其他代码,您可能还可以使用以下代码(这取决于您的其余代码...我只是将其扔掉,因为您并不真正向我们展示了很多... enqueue 函数可能是更好的选择):

<script type="text/javascript" src="<?php echo osc_current_web_theme_js_url('jquery.js') ; ?>"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2015-06-09
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多