【问题标题】:How do I fix my jQuery to stop giving an error [closed]如何修复我的 jQuery 以停止给出错误 [关闭]
【发布时间】:2015-10-09 03:41:33
【问题描述】:

错误消息说“无法获取未定义或空引用的属性'top'。第13行字符9

/*!
 * Start Bootstrap - Creative Bootstrap Theme (http://startbootstrap.com)
 * Code licensed under the Apache License v2.0.
 * For details, see http://www.apache.org/licenses/LICENSE-2.0.
 */

(function($) {
    "use strict"; // Start of use strict

    // jQuery for page scrolling feature - requires jQuery Easing plugin
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: ($($anchor.attr('href')).offset().top - 50)
        }, 1250, 'easeInOutExpo');
        event.preventDefault();
    });

    // Highlight the top nav as scrolling occurs
    $('body').scrollspy({
        target: '.navbar-fixed-top',
        offset: 51
    })

    // Closes the Responsive Menu on Menu Item Click
    $('.navbar-collapse ul li a').click(function() {
        $('.navbar-toggle:visible').click();
    });

    // Fit Text Plugin for Main Header
    $("h1").fitText(
        1.2, {
            minFontSize: '35px',
            maxFontSize: '65px'
        }
    );

    // Offset for Main Navigation
    $('#mainNav').affix({
        offset: {
            top: 100
        }
    })

    // Initialize WOW.js Scrolling Animations
    new WOW().init();

})(jQuery); // End of use strict

每当我点击此链接时,我都会收到 JavaScript 错误

<a href="#about" class="btn btn-primary btn-xl page-scroll">OCTOBER 9, 2015 12 - 2 PM<br>JURY ASSEMBLY</a>

请帮忙

【问题讨论】:

  • 听起来$($anchor.attr('href')) 返回的是空对象。无论如何,您应该发布简约样本复制问题...顺便说一句,SO 不是please-debug-my-code.com

标签: javascript jquery html


【解决方案1】:

您可能想检查是否存在 id="about" 的对象。 当我添加您的代码有效时...

<div id="about">this is about div</div>

jquery 无法获取不存在的对象的偏移量...

http://jsfiddle.net/jkrielaars/3gx4atdt/

【讨论】:

  • 更准确地说,它从 offset() 方法返回 undefined 为空 jq 对象,因此为 top 属性抛出此错误
猜你喜欢
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 2019-02-18
  • 2015-11-05
  • 1970-01-01
相关资源
最近更新 更多