【问题标题】:None of the jQuery scripts on my Wordpress site are working我的 Wordpress 网站上的所有 jQuery 脚本都不起作用
【发布时间】:2013-10-10 13:44:04
【问题描述】:

我有一个使用修改过的主题和一些插件的网站。我的js知识一点都不好,所以如果以前出现过这种情况,请接受我的歉意。 http://www.georgvoros.com/

似乎没有任何 js 正在运行,包括滑块、导航下拉菜单和灯箱脚本。我已将 Wordpress 更新为 3.61,将 jQuery 更新为 1.10.2。

我在 Chrome 检查元素窗口中收到以下错误。

Uncaught TypeError: Cannot read property 'msie' of undefined jquery.tools.min.js?ver=1.2.7:13

Uncaught TypeError: Cannot set property 'touchstart' of undefined jquery.mobile.customized.min.js:10

Uncaught TypeError: Object function (b,c,d){var e=b.split(".")[0],f;b=b.split(".")[1];f=e+"-"+b;if(!d){d=c;c=a.Widget}a.expr[":"][f]=function(c){return!!a.data(c,b)};a[e]=a[e]||{};a[e][b]=function(a,b){if(arguments.length){this._createWidget(a,b)}};var g=new c;g.options=a.extend(true,{},g.options);a[e][b].prototype=a.extend(true,g,{namespace:e,widgetName:b,widgetEventPrefix:a[e][b].prototype.widgetEventPrefix||b,widgetBaseClass:f},d);a.widget.bridge(b,a[e][b])} has no method 'extend' jquery-ui.js?ver=3.6.1:539

我已阅读有关“$”变量导致问题的信息,但我不明白我需要在哪里进行更改。

head.php 中如下

    <script type="text/javascript">
    // initialise plugins
        jQuery(function(){
            // main navigation init
             jQuery('ul.sf-menu').superfish({
                delay:       <?php echo of_get_option('sf_delay'); ?>,      // one second delay on mouseout 
                animation:   {opacity:'<?php echo of_get_option('sf_f_animation'); ?>'<?php if (of_get_option('sf_sl_animation')=='show') { ?>,height:'<?php echo of_get_option('sf_sl_animation'); ?>'<?php } ?>}, // fade-in and slide-down animation
                speed:       '<?php echo of_get_option('sf_speed'); ?>',  // faster animation speed 
                autoArrows:  <?php echo of_get_option('sf_arrows'); ?>,   // generation of arrow mark-up (for submenu)
                dropShadows: false
            }); 

            // prettyphoto init
            jQuery("a[rel^='prettyPhoto']").prettyPhoto({
                animation_speed:'normal',
                slideshow:5000,
                autoplay_slideshow: false,
                overlay_gallery: true
            });

            // Initialize the gallery
            jQuery("#gallery .touch-item").touchTouch();


        });

</script>
        <script type="text/javascript">

        // Init for audiojs
        audiojs.events.ready(function() {
            var as = audiojs.createAll();
        });

        // Init for si.files
        SI.Files.stylizeAll();
        $(function(){
            $('.sf-menu > li').append('<span class="bg-menu"></span>');
            $('.sf-menu > li:last').addClass('last');
            $('.recent-posts.services li:nth-child(3n)').addClass('nomargin');
            if ($.browser.msie && $.browser.version < 10) {
                jQuery('input[type="submit"], input[type="reset"]').hover(function(){
                    jQuery(this).addClass('submit-hover')
                 },
                     function(){
                      jQuery(this).removeClass('submit-hover')
                 });    
            }
        });

        jQuery(function() {

            $('.right-content-area #widget_search.box h3').each(function(index) {
                //get the first word
                var firstWord = $(this).text().split(' ')[0];

                //wrap it with span
                var replaceWord = "<span>" + firstWord + "</span>";

                //create new string with span included
                var newString = $(this).html().replace(firstWord, replaceWord);

                //apply to the divs
                $(this).html(newString);
            });

        });
    </script>

在theme-scripts.php 中是:

    <?php
/*  Register and load javascript
/*-----------------------------------------------------------------------------------*/
function my_script() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', get_bloginfo('template_url').'/js/jquery-1.10.2.min.js', false, '1.10.2');
        wp_enqueue_script('jquery');

        wp_enqueue_script('modernizr', get_bloginfo('template_url').'/js/modernizr.js', array('jquery'), '2.6.2');
        wp_enqueue_script('superfish', get_bloginfo('template_url').'/js/superfish.js', array('jquery'), '1.7.4');
        wp_enqueue_script('easing', get_bloginfo('template_url').'/js/jquery.easing.1.3.js', array('jquery'), '1.3');
        wp_enqueue_script('prettyPhoto', get_bloginfo('template_url').'/js/jquery.prettyPhoto.js', array('jquery'), '3.1.5');
        wp_enqueue_script('tools', get_bloginfo('template_url').'/js/jquery.tools.min.js', array('jquery'), '1.2.7');
        wp_enqueue_script('mobilemenu', get_bloginfo('template_url').'/js/jquery.mobilemenu.js', array('jquery'), '1.1');
        wp_enqueue_script('loader', get_bloginfo('template_url').'/js/jquery.loader.js', array('jquery'), '1.0');
        wp_enqueue_script('swfobject', get_bloginfo('url').'/wp-includes/js/swfobject.js', array('jquery'), '2.3');
        wp_enqueue_script('twitter', get_bloginfo('template_url').'/js/jquery.twitter.js', array('jquery'), '1.0');
        wp_enqueue_script('flickr', get_bloginfo('template_url').'/js/jquery.flickrush.js', array('jquery'), '1.0');
        wp_enqueue_script('touch', get_bloginfo('template_url').'/js/touchTouch.jquery.js', array('jquery'), '1.0');
        wp_enqueue_script('si_files', get_bloginfo('template_url').'/js/si.files.js', array('jquery'), '1.0');
        wp_enqueue_script('audiojs', get_bloginfo('template_url').'/js/audiojs/audio.js', array('jquery'), '1.0');
        wp_enqueue_script('custom', get_bloginfo('template_url').'/js/custom.js', array('jquery'), '1.0');
        wp_enqueue_script('camera', get_bloginfo('template_url').'/js/camera.js', array('jquery'), '1.3.3');
    }
}
add_action('init', 'my_script');


/*  Register and load admin javascript
/*-----------------------------------------------------------------------------------*/

function tz_admin_js($hook) {
    if ($hook == 'post.php' || $hook == 'post-new.php') {
        wp_register_script('tz-admin', get_template_directory_uri() . '/js/jquery.custom.admin.js', 'jquery');
        wp_enqueue_script('tz-admin');
    }
}
add_action('admin_enqueue_scripts','tz_admin_js',10,1);
?>

我也看不到该站点正在加载多个版本的 jQuery。

【问题讨论】:

  • 如果 jQuery.browser 未定义,您使用的脚本很可能依赖于早期版本的 jQuery(但 $.browser 自 1.9 以来已消失)。尝试包括 jQuery Migrate(找到 @jquery.com)
  • 对我来说似乎是您的jquery-ui.js ->http://www.georgvoros.com/wp-content/plugins/form-maker/js/jquery-ui.js?ver=3.6.1 损坏或太旧了。而你所有的错误都对应于 UI 插件 ...

标签: javascript php jquery html wordpress


【解决方案1】:

看起来您的网站依赖于很多旧的 jQuery(例如 jQuery 工具现在不是很常用,并且由于维护更完善的 jQuery UI 的流行,已经好几年没有使用了)。更新到最新版本的 jQuery 可能刚刚破坏了大部分过时的脚本。

您或许可以通过简单地包含 jQuery Migrate 官方插件来修复它,该插件为以前版本中已弃用或删除的旧 jQuery 功能添加了向后兼容性。

只需在您的 jQuery wp_enqueue_script() 调用之后立即将其包含在 wp_enqueue_script() 中,并且运气好的话可以解决您的许多问题(如果不是全部的话)。

如果这不能解决问题,您将需要一一解决问题,并使用更现代的技术重新实现它们或更新它们各自的脚本依赖项。

【讨论】:

  • +1 用于迁移插件。我对旧版本的 fancybox 和更新的 jquery 版本也有类似的问题,事实上这是因为 $.browser 不再定义了。迁移插件为我解决了这个问题。
  • 所以我犯了一个菜鸟错误,因为我认为服务器更新破坏了我的脚本并更新了 wordpress 和所有插件,而我实际上刚刚安装了 Form Maker 插件。我已使用以下内容更新了 theme-scripts.php 文件:code wp_deregister_script('jquery'); wp_register_script('jquery', get_bloginfo('template_url').'/js/jquery-1.10.2.min.js', false, '1.10.2'); wp_enqueue_script('jquery'); wp_enqueue_script('migrate', '//code.jquery.com/jquery-migrate-1.2.1.min.js', array('jquery'), '1.2.1')​​; code 仍未修复。我将删除 Form Maker 并重试。 tks
  • 感谢@ennui 的帮助。我的移动菜单出现了不应该出现的故障,但我相信我会找到解决此问题的方法。
【解决方案2】:

这些错误是由于您的 jQuery 版本中缺少某些功能造成的...

jQuery.browser(或$.browser)在 jQuery 1.9 版中被删除。您网站中的某些代码似乎是为旧版本的 jQuery 编写的。

您可以降级到早期版本,或更新您网站上的 Javascript 以使用 jQuery 1.10.2。

【讨论】:

  • @davidkonrad 什么运行 1.10.2? OPs 网站上有很多插件依赖于旧版本的 jQuery,但加载的版本是 1.10.2。
  • @davidkonrad 是的。做得好。这就是问题所在。
  • 是的,但引用 $.browser 并不会倾斜整个插件范围
  • @davidkonrad 删除你的第一个 cmets 让我的想法变得荒谬。
  • 对不起 - 这是一条无用的评论,因此我删除了它
猜你喜欢
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 2023-03-10
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多