JQuery not working in <head>?

This code should make a slide show of the stacked elements of the list (I commented on CSS so that I can see what is happening), fading out the top elements until only the first is visible, and then disappears in the top element and everything else and start over. If I put a script below my contents inside <body>and throw $(function() {it away , it works fine, but <head>nothing happens. I wrote this yesterday, and today I still do not see the error, so I post it here.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        ul {
            position: relative;
        }
        ul li {
            /*position: absolute;
            left: 0;
            top: 0;*/
        }
    </style>
    <script src="jquery-1.5.1.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(function() {
            var i = 0;
            var count = $('ul li').size();      

            function fade() {
                if (i < count-1) {
                $('ul li:nth-child('+(count-i)+')').fadeOut(300);
                    i++;
                } else {
                    $('ul li:nth-child('+count+')').fadeIn(300, function(){$('ul li').show();});    
                    i = 0;
                }
            }

            $('button').click(function() {
                setInterval('fade()', 1000);
            });
        });
    </script>
</head>

<body>
    <button>Slideshow GO!</button>
    <ul id="slider">
        <li><img src="1.jpg" /></li>
        <li><img src="2.jpg" /></li>    
        <li><img src="3.jpg" /></li>
        <li><img src="4.jpg" /></li>
    </ul>
</body>
</html>

thank

+3
source share
3 answers

, $(function () {}) , fade , . setTimeout 'fade()' , .

, setTimeout , :

setTimeout(fade, 1000);

, setTimeout.

+4

jQuery DOM $(document).ready(). javascript :

var i = 0;
var count;

function fade() {
    if (i < count-1) {
        $('ul li:nth-child('+(count-i)+')').fadeOut(300);
            i++;
        } else {
            $('ul li:nth-child('+count+')').fadeIn(300, function(){$('ul li').show();});    
            i = 0;
        }
    }
}

$(document).ready(function() {
    count = $('ul li').size();
    $('button').click(function() {
        setInterval('fade()', 1000);
    });
} );

, i, count fade $(document).ready, . , setInterval .

jQuery , , , , , . $(document).ready , DOM, , .

0

JavaScript , , :

fade is not defined
setInterval('fade()', 1000); 

:

    $(function() {
        var i = 0;
        var count = $('ul li').size();      


        $('button').click(function() {
            setInterval(function(){
                if (i < count-1) {
                $('ul li:nth-child('+(count-i)+')').fadeOut(300);
                    i++;
                } else {
                    $('ul li:nth-child('+count+')').fadeIn(300, function(){$('ul li').show();});    
                    i = 0;
                }
            }, 1000);
        });
    });

setInterval .

0

All Articles