为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Smart猫小萌 2021-08-20 18:59:39
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'));                        });                    }                });            }        },
查看完整描述

1 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

因为在您的counterupActivation方法中,您有这一行:


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

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

});

注意这一行:


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

这行代码的作用是将HTML 中的所有内容替换为一个新值。意思是,你&#37;正在被覆盖。


您需要做的是&#37;从您的 HTML 中删除,并将其附加到您上面的行中:


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

    $(this).html($(this).data('count-to') + '&#37;');

});


查看完整回答
反对 回复 2021-08-20
  • 1 回答
  • 0 关注
  • 132 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信