猿问

为什么我的百分比符号没有与整数值一起输出?

HTML 代码<span class="odometer" data-count-to="100"></span>输出 100。我希望它计数并显示百分比符号。例如 100%。我该怎么做?


我曾尝试使用 using&#37;来表示%符号,但它不起作用。<span class="odometer" data-count-to="100">&#37;</span>


输出的全部是 100,最后没有百分号。


  /* Countdown Activation */

        countdownActivation: function () {

            $('.tm-countdown').each(function () {

                var $this = $(this),

                    finalDate = $(this).data('countdown');

                $this.countdown(finalDate, function (event) {

                    $this.html(event.strftime(

                        '<div class="tm-countdown-pack tm-countdown-day"><h2 class="tm-countdown-count">%-D</h2><h5>Days</h5></div><div class="tm-countdown-pack tm-countdown-hour"><h2 class="tm-countdown-count">%-H</h2><h5>Hour</h5></div><div class="tm-countdown-pack tm-countdown-minutes"><h2 class="tm-countdown-count">%M</h2><h5>Min</h5></div><div class="tm-countdown-pack tm-countdown-seconds"><h2 class="tm-countdown-count">%S</h2><h5>Sec</h5></div>'));

                });

            });

        },


        /* CounterUp Activation */

        counterupActivation: function () {

            if ($('.odometer').length) {

                $(window).on('scroll', function () {

                    function winScrollPosition() {

                        var scrollPos = $(window).scrollTop(),

                            winHeight = $(window).height();

                        var scrollPosition = Math.round(scrollPos + (winHeight / 1.2));

                        return scrollPosition;

                    }

                    var elemOffset = $('.odometer').offset().top;

                    if (elemOffset < winScrollPosition()) {


                        $('.odometer').each(function () {

                            $(this).html($(this).data('count-to'));

                        });

                    }

                });

            }

        },



Smart猫小萌
浏览 133回答 1
1回答

红颜莎娜

因为在您的counterupActivation方法中,您有这一行:$('.odometer').each(function () {&nbsp; &nbsp; $(this).html($(this).data('count-to'));});注意这一行:$(this).html($(this).data('count-to'));这行代码的作用是将HTML 中的所有内容替换为一个新值。意思是,你&#37;正在被覆盖。您需要做的是&#37;从您的 HTML 中删除,并将其附加到您上面的行中:$('.odometer').each(function () {&nbsp; &nbsp; $(this).html($(this).data('count-to') + '&#37;');});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答