Java脚本新手-在中使用而不是在函数.php中使用,加载不正确

时间:2013-03-01 作者:user1255049

Live site.

我决定在我的新主题中加入javascript;这是我第一次这样做,并通过博客和论坛将我的想法拼凑起来。我将javascript放在标记中,而不是函数中。php。我注意到,在由javascript控制的导航之后,网站停止加载,这让我相信这就是问题所在,我只是不知道如何准确地找出到底是什么导致了问题。非常感谢您的帮助。

<!DOCTYPE html>
<html>
<head>
        <meta http-equiv="Content-Type" content="<?php bloginfo(\'html_type\'); ?>; charset=<?php bloginfo(\'charset\'); ?>" />
        <meta name="generator" content="Wordpress <?php bloginfo(\'version\'); ?>" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title><?php bloginfo(\'name\'); ?> <?php wp_title(); ?></title>
        <link rel="pingback" href="<?php bloginfo(\'pingback url\'); ?>" />
        <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php bloginfo(\'rss2_url\'); ?>" />
        <link rel="stylesheet" href="<?php bloginfo(\'stylesheet_url\'); ?>" />
        <link rel="shortcut icon" href="<?php bloginfo(\'stylesheet_directory\'); ?>/favicon.ico" />
        <?php wp_head(); ?>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
            $(window).load(function(event) {
                $(\'#Nav a\').click(function() {
                    $(\'#Nav li\').removeClass();
                    $(this).parent().addClass("current");                                      
                });
        </script>
        <script type="text/javascript">
                var _gaq = _gaq || [];
                _gaq.push([\'_setAccount\', \'UA-32783537-1\']);
                _gaq.push([\'_trackPageview\']);

                (function() {
                var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
                ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
                var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
                })();
        </script>
        <script>
                $(document).ready(function() {
                  function filterPath(string) {
                  return string
                    .replace(/^\\//,\'\')
                    .replace(/(index|default).[a-zA-Z]{3,4}$/,\'\')
                    .replace(/\\/$/,\'\');
                  }
                  var locationPath = filterPath(location.pathname);
                  var scrollElem = scrollableElement(\'html\', \'body\');

                  $(\'a[href*=#]\').each(function() {
                    var thisPath = filterPath(this.pathname) || locationPath;
                    if (  locationPath == thisPath
                    && (location.hostname == this.hostname || !this.hostname)
                    && this.hash.replace(/#/,\'\') ) {
                      var $target = $(this.hash), target = this.hash;
                      if (target) {
                        var targetOffset = $target.offset().top;
                        $(this).click(function(event) {
                          event.preventDefault();
                          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
                            location.hash = target;
                          });
                        });
                      }
                    }
                  });

                  // use the first element that is "scrollable"
                  function scrollableElement(els) {
                    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
                      var el = arguments[i],
                          $scrollElement = $(el);
                      if ($scrollElement.scrollTop()> 0) {
                        return el;
                      } else {
                        $scrollElement.scrollTop(1);
                        var isScrollable = $scrollElement.scrollTop()> 0;
                        $scrollElement.scrollTop(0);
                        if (isScrollable) {
                          return el;
                        }
                      }
                    }
                    return [];
                  }

                });
        </script>
        <script>
                $(function() {
                    $.fn.scrollBottom = function() {
                        return $(document).height() - this.scrollTop() - this.height();
                    };

                    var $el = $(\'#nav>div\');
                    var $window = $(window);
                    var top = $el.parent().position().top;

                    $window.bind("scroll resize", function() {
                        var gap = $window.height() - $el.height() - 10;
                        var visibleFoot = 340 - $window.scrollBottom();
                        var scrollTop = $window.scrollTop()

                        if (scrollTop < top + 10) {
                            $el.css({
                                top: (top - scrollTop) + "px",
                                bottom: "auto"
                            });
                        } else if (visibleFoot > gap) {
                            $el.css({
                                top: "auto",
                                bottom: visibleFoot + "px"
                            });
                        } else {
                            $el.css({
                                top: 0,
                                bottom: "auto"
                            });
                        }
                    }).scroll();
                });
        </script>
</head>

1 个回复
SO网友:Matt van Andel

首先,将您的JS放在适当的位置。主题目录中的js文件wp_enqueue_scripts 钩子(这是您将排队/加载任何自定义Javascript的地方)wp_enqueue_script() 加载脚本示例:

add_action( \'wp_enqueue_scripts\', \'enqueue_my_stuff\' );
function enqueue_my_stuff () {
    wp_enqueue_script(\'slug_for_your_script\' , get_template_directory_uri() . \'/path/to/yourscripts.js\', array(\'jquery\') );
}

结束

相关推荐

动态观察滑块和事件管理器插件JavaScript

我有这个滑块(环绕滑块http://foundation.zurb.com/docs/orbit.php ) 和wordpress插件(事件管理器http://wp-events-plugin.com/ ) 加载所有jQuery ui内容(/** * Enqueing public scripts and styles */ function public_enqueue() { //Scripts wp_enqueue_script