javascript - 动画计算 JSON 值的数量并替换 null

标签 javascript json jquery-animate isnull

可否实现json数据的动画计数js。我试过了,但正在返回 Nan。这是我的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="number"><span class="count">Count</span></div>
<div id="id">Id: </div>
<div id="pos">POS: </div>

<div> Static Number</div>
<div class="count">345678912</div>
<script type="text/javascript">
    $('.count').each(function () {
    $(this).prop('Counter',0).animate({
        Counter: $(this).text()
    }, {
        duration: 4000,
        easing: 'swing',
        step: function (now) {
            $(this).text(Math.ceil(now));
        }
    });
});
    </script>

JS:

var info = {"city":[
{"id":"4","number":"376478","pos":"null"},
{"id":"5","number":"4565878","pos":"3"},]};

var res = info.city.filter(function(item){
  return item.id=='4';
  function CheckNullReturnBlank(item) {
    return item = (item == null) ? 'No Data' : item;
}
});
console.log(res);
var HTML = '<span>'+res[0].number+'</span>';
$('.count').append(HTML);
var HTML ='<span>'+res[0].id+'</span>';
$('#id').append(HTML);
var HTML ='<span>'+res[0].pos+'</span>';
$('#pos').append(HTML);

但如果我使用静态数字进行测试,则动画效果完美。另一件事是,我试图将 null 值从 json 替换为 No Data Available 但没有成功。感谢我可以尝试的任何帮助。

这是 JSfiddle 链接:http://jsfiddle.net/zeef/vLw5wo5p/3/

最佳答案

有一些问题:

  • 你写 item == null ,而“null”是一个字符串。
  • 你的项目是一个对象,你需要的是它的属性(item.pos == 'null')
  • 你得到 NaN 因为在你的第一个 .count 范围内你有“Count”字符串,它不是一个数字 :)
  • 您的过滤器函数永远不会到达 CheckNullReturnBlank 函数;
  • 并且您将动画脚本放在 HTML 中,因此它会在您的其他 Javascript 之前加载,因此动态添加的内容不会受到 HTML 中脚本的影响。

var info = {"city":[
{"id":"4","number":"376478","pos":"null"},
{"id":"5","number":"4565878","pos":"3"},]};

var res = info.city.filter(function(item){
  return item.id=='4';
});

res.forEach(function(item) {
	item.pos = (item.pos == 'null') ? 'No Data' : item.pos;
});

console.log(res);
var HTML = '<span>'+res[0].number+'</span>';
$('.count').append(HTML);
var HTML ='<span>'+res[0].id+'</span>';
$('#id').append(HTML);
var HTML ='<span>'+res[0].pos+'</span>';
$('#pos').append(HTML);

$('.count').each(function () {
  $(this).prop('Counter',0).animate({
      Counter: $(this).text()
  }, {
      duration: 4000,
      easing: 'swing',
      step: function (now) {
          $(this).text(Math.ceil(now));
      }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="number">Count <span class="count"></span></div>
<div id="id">Id: </div>
<div id="pos">POS: </div>

<div> Static Number</div>
<div class="count">345678912</div>

关于javascript - 动画计算 JSON 值的数量并替换 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47811155/

相关文章:

javascript - 在一个脚本中自动刷新和自动上下滚动

javascript - 如何压缩一个javascript文件?

javascript - 如何使用 JavaScript 制作动态下拉菜单

json - 为什么在接口(interface)中添加 func 会导致 json.Unmarshal 失败?

javascript - 向下滚动时如何创建一个小窗口?

javascript - 右键单击 Ant Design 的折叠菜单项不显示 "Open in new tab"

json - 如何在 Dart 中使用带有 json 序列化的泛型和泛型列表?

javascript - Ajax 结果不是对象

javascript - 使用 setTimeout 函数。和随机函数。为类元素添加动画

Jquery 动画在点击时放大/缩小 div(2 次点击)