javascript - Unable to get property 'top' of undefined or null reference (IE 11) - Stack Overflow

I'm pretty inexperienced with JavaScript and am using a template. Cannot seem to figure out why th

I'm pretty inexperienced with JavaScript and am using a template. Cannot seem to figure out why this error appears in Internet Explorer. It works in every other browser.

$('.navbar a, .navbar li a, .brand, #footer li a, .more a, a.go-top')
  .bind('click', function(event) {
    var $anchor = $(this),
    scrollVal = $($anchor.attr('href')).offset().top - 60;

    if (scrollVal < 0) {
      scrollVal = 0;
    }

    $('[data-spy="scroll"]').each(function() {
      $(this).scrollspy('refresh');
    });

    $.scrollTo(scrollVal, {
      easing: 'easeInOutExpo',
      duration: 1500
    });

    event.preventDefault();
  });

Any ideas why this is happening?

I'm pretty inexperienced with JavaScript and am using a template. Cannot seem to figure out why this error appears in Internet Explorer. It works in every other browser.

$('.navbar a, .navbar li a, .brand, #footer li a, .more a, a.go-top')
  .bind('click', function(event) {
    var $anchor = $(this),
    scrollVal = $($anchor.attr('href')).offset().top - 60;

    if (scrollVal < 0) {
      scrollVal = 0;
    }

    $('[data-spy="scroll"]').each(function() {
      $(this).scrollspy('refresh');
    });

    $.scrollTo(scrollVal, {
      easing: 'easeInOutExpo',
      duration: 1500
    });

    event.preventDefault();
  });

Any ideas why this is happening?

Share Improve this question edited Jun 9, 2014 at 16:30 enter_the_natrix asked Jun 9, 2014 at 15:08 enter_the_natrixenter_the_natrix 511 silver badge4 bronze badges 5
  • 1 $($anchor.attr('href')) doesn't find any matching element. You need to do some debugging. What is the value of $anchor.attr("href")? Is it a tag name? When you get errors, use console.log() to confirm that the values in your program are what you expect. – cookie monster Commented Jun 9, 2014 at 15:10
  • 2 And are you saying it works in other browsers, just not IE11? You have almost enough information in your question for it to be answered, but not quite. Most questions dealing with DOM selection will need to include the HTML used to create the DOM. – cookie monster Commented Jun 9, 2014 at 15:15
  • Well there are multiple links that use the scroll function in the html page... I can't really put in the entire page can I? – enter_the_natrix Commented Jun 9, 2014 at 16:31
  • 1 What you need to do is first debug, and then if you still can't figure it out, show the results of your debugging in the question, and narrow the HTML and JS down to a minimal example that successfully demonstrates the issue. – cookie monster Commented Jun 9, 2014 at 16:35
  • @Ejay give me a break please I am working on it. :( – enter_the_natrix Commented Jun 9, 2014 at 17:14
Add a ment  | 

1 Answer 1

Reset to default 3

the error you are seeing is in the line 4

    scrollVal = $($anchor.attr('href')).offset().top - 60;

it is a monly because you are trying to use a propierty of an object and it is undefined.

in your case $($anchor.attr('href')).offset() is probably undefined, you need to see if $anchor is undefined or it not have the propierty href so it can't have the ofset

you can use developer tools (F12) and a breack point to inspect the values.

you can learn how to do it in: how to use console

good luck

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745361222a4624368.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信