Jquery功能没有显示,怎么来的?

时间:2012-07-09 10:33:58

标签: jquery html

我正在努力让这个Jquery计数功能正常工作,但它并没有出现在我身上,有人可以告诉我我做错了什么吗?

我正在使用由“mhuggins”(https://github.com/mhuggins/jquery-countTo)创建的函数,所以我自己没有做过,只是试图让它工作。

  

countup.js

    (function($) {
    $.fn.countTo = function(options) {
        // merge the default plugin settings with the custom options
        options = $.extend({}, $.fn.countTo.defaults, options || {});

        // how many times to update the value, and how much to increment the value on each update
        var loops = Math.ceil(options.speed / options.refreshInterval),
            increment = (options.to - options.from) / loops;

        return $(this).each(function() {
            var _this = this,
                loopCount = 0,
                value = options.from,
                interval = setInterval(updateTimer, options.refreshInterval);

            function updateTimer() {
                value += increment;
                loopCount++;
                $(_this).html(value.toFixed(options.decimals));

                if (typeof(options.onUpdate) == 'function') {
                    options.onUpdate.call(_this, value);
                }

                if (loopCount >= loops) {
                    clearInterval(interval);
                    value = options.to;

                    if (typeof(options.onComplete) == 'function') {
                        options.onComplete.call(_this, value);
                    }
                }
            }
        });
    };

    $.fn.countTo.defaults = {
        from: 0,  // the number the element should start at
        to: 100,  // the number the element should end at
        speed: 1000,  // how long it should take to count between the target numbers
        refreshInterval: 100,  // how often the element should be updated
        decimals: 0,  // the number of decimal places to show
        onUpdate: null,  // callback method for every time the element is updated,
        onComplete: null,  // callback method for when the element finishes updating
    };
})(jQuery);

然后我试图在这个html页面中使用它

  

的test.html

<html>
<head>
<script type="text/javascript" src="scripts/jquery-1.6.2.js"></script>
<script type="text/javascript" src="scripts/countup.js"></script>
<script type="text/javascript"><!--
    $('.timer').countTo({from: 0, to: 500});
//--></script>
</head>

<body>
<span class="timer"></span>
</body>
</html>

当我启动页面时,没有任何反应。这个剧本适合其他人,所以很明显我做错了什么。我对此非常陌生,所以这并不奇怪。

谢谢!

3 个答案:

答案 0 :(得分:4)

您似乎缺少document.ready(function() { // timer code here });

答案 1 :(得分:2)

执行脚本时没有<span class="timer">。将其添加为DOMready handler

<script type="text/javascript"><!--
    jQuery(function($) {
        $('.timer').countTo({from: 0, to: 500});
    });
//--></script>
</head>

或在身体的末尾执行它:

<body>
    <span class="timer"></span>
    <script type="text/javascript"><!--
        $('.timer').countTo({from: 0, to: 500});
    //--></script>
</body>

答案 2 :(得分:1)

$(document).ready(function(){
    $('.timer').countTo({from: 0, to: 500});
});