admin管理员组

文章数量:1297116

The javascript, html and css work in this jsfiddle but when entered into an html file like so:

<!doctype html>
<html>
  <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="chrome=1">
        <script src=".11.3.min.js"></script>
        <script type="text/javascript">
            var target = $(".mypara").offset().top;
            var interval = setInterval(function() {
                if ($(window).scrollTop() >= target) {
                    alert("made it!");
                    clearInterval(interval);
                    }
            }, 250);
        </script>
        <style>
            body {
            background-color: linen;
            height: 1000px;
            }
            p {
             color: blue;
              margin-top: 500px;
            }
        </style>

    </head>
    <body>
     <p class="mypara">asdfasdfasf</p>
    </body>
</html>

chrome console gives this error

Uncaught TypeError: Cannot read property 'top' of undefined(anonymous function) @ index - Copy.html:8

This error refers to line 8:

var target = $(".mypara").offset().top;

Can someone help me understand why?

The javascript, html and css work in this jsfiddle but when entered into an html file like so:

<!doctype html>
<html>
  <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="chrome=1">
        <script src="http://ajax.aspnetcdn./ajax/jQuery/jquery-1.11.3.min.js"></script>
        <script type="text/javascript">
            var target = $(".mypara").offset().top;
            var interval = setInterval(function() {
                if ($(window).scrollTop() >= target) {
                    alert("made it!");
                    clearInterval(interval);
                    }
            }, 250);
        </script>
        <style>
            body {
            background-color: linen;
            height: 1000px;
            }
            p {
             color: blue;
              margin-top: 500px;
            }
        </style>

    </head>
    <body>
     <p class="mypara">asdfasdfasf</p>
    </body>
</html>

chrome console gives this error

Uncaught TypeError: Cannot read property 'top' of undefined(anonymous function) @ index - Copy.html:8

This error refers to line 8:

var target = $(".mypara").offset().top;

Can someone help me understand why?

Share Improve this question edited Aug 4, 2015 at 1:41 aquagremlin asked Aug 4, 2015 at 1:35 aquagremlinaquagremlin 3,5495 gold badges31 silver badges55 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Wrap your code in

$(document).ready (function (){
    // code here
});

You're trying to access an element in the DOM before it exists so when your trying to access the class the item doesnt exist yet. Or move your script below the elements in the html

Works in fiddle cause thet wrap you're code depending on your setting which defaults to domready I believe

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="chrome=1">

        <style>
            body {
                background-color: linen;
                height: 1000px;
            }
            p {
                color: blue;
                margin-top: 500px;
            }
        </style>
    </head>

    <body>
        <p class="mypara">asdfasdfasf</p>
        <p class="mypara">Include js files to be at the bottom so that it would be the one to be loaded accordingly</p>
        <script src="http://ajax.aspnetcdn./ajax/jQuery/jquery-1.11.3.min.js"></script>
        <script type="text/javascript">
            // if document is ready then
            // its the only time to execute
            // process withing the block
            $(document).ready(function() {
                var target = $(".mypara").offset().top;
                var interval = setInterval(function() {
                    if ($(window).scrollTop() >= target) {
                        alert("made it!");
                        clearInterval(interval);
                    }
                }, 250);
            });

            </script>
    </body>
</html>

本文标签: jqueryjavascript error 39cannot read property of undefined39Stack Overflow