如何在JavaScript中的数字末尾添加“ +”

问题描述 投票:-4回答:1

我有一段代码,当用户滚动到它时开始递增。代码段代码不是我的。作者是戴维·里斯(David Reis)。但这与我所做的非常相似。

var a = 0;
$(window).scroll(function() {

  var oTop = $('#counter').offset().top - window.innerHeight;
  if (a == 0 && $(window).scrollTop() > oTop) {
    $('.counter-value').each(function() {
      var $this = $(this),
        countTo = $this.attr('data-count');
      $({
        countNum: $this.text()
      }).animate({
          countNum: countTo
        },

        {

          duration: 2000,
          easing: 'swing',
          step: function() {
            $this.text(Math.floor(this.countNum));
          },
          complete: function() {
            $this.text(this.countNum);
            //alert('finished');
          }

        });
    });
    a = 1;
  }

});
.fake-div {
  width: 100%;
  height: 1280px;
  position: relative;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="fake-div">
</div>
<div id="counter">
  <div class="counter-value" data-count="300">0</div>
  <div class="counter-value" data-count="400">100</div>
  <div class="counter-value" data-count="1500">200</div>
</div>
<div class="fake-div">
</div>

但是最后,当结束时,我希望他们在其末尾加上一个“ +”。

例如,我希望最终结果看起来像下面的数字:300+ 400+ 1500+

javascript html css increment auto-increment
1个回答
0
投票

只需将字符串添加到数字的末尾

var变量=数字+“ +”;

或者,如果您想看的话

function myFunction(){

var str1 = "Hello ";
var str2 = "world!";
var str3 = " Have a nice day!";
var res = str1.concat(str2, str3);
document.getElementById("demo").innerHTML = res;

}

这正在使用javascript concat()方法,但在您的情况下可能会过大。

© www.soinside.com 2019 - 2024. All rights reserved.