如何对由分隔的数字使用jquery函数.(点)或,(逗号)作为千分隔符
How to use jquery function on numbers separated by .(dot) or ,(comma) as thousands separator
我有一个特定的数字,我使用计数器jquery函数,以便在特定的时间段内,该数字将从0变为该数字。现在,问题是当我添加(逗号)或时。(点)到数字作为千分隔符,它不起作用。如何使用、(逗号)和使脚本工作。(点)分隔符,并使用、(逗号)和返回结果。(点)也?
html
<div class='number_container'>
<span class='count'>317249</span>
</div>
css
.number_container {
background: red;
width: 100px;
text-align: center;
padding: 38px 0;
border-radius: 50px;
color: white;
margin: 0 auto;
}
.count {
font: 20px arial;
}
Js
$(document).ready(function() {
$('.count').each(function() {
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 3500,
easing: 'swing',
step: function(now) {
$(this).text(Math.ceil(now));
}
});
});
});
Jsfidle预览
https://jsfiddle.net/y6zdmaeq/
我希望数字317249显示为317249或317.249,并从0开始向上计数。感谢
结合这个答案和您的代码,只需在.text()
之前格式化代码
https://jsfiddle.net/y6zdmaeq/2/
我已经为您的需求编码了html。请将其保存为html文件,然后在浏览器中打开。如果你喜欢,请投票。请注意,我已经包含了一个相同的外部插件。"编码快乐!!"
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="http://www.bendewey.com/code/formatcurrency/jquery.formatCurrency-1.4.0.js"></script>
</head>
<script>
$(document).ready(function() {
$('.count').each(function() {
$(this).prop('Counter', 0).animate({
Counter: $(this).text()
}, {
duration: 3500,
easing: 'swing',
step: function(now) {
$("#hiddenInput").val(Math.ceil(now).toString());
var formattedText = $(".currency").formatCurrency().val();
//alert(formattedText);
$(this).text(formattedText);
}
});
});
});
</script>
<style>
.number_container {
background: red;
width: 100px;
text-align: center;
padding: 38px 0;
border-radius: 50px;
color: white;
margin: 0 auto;
}
.count {
font: 20px arial;
}
</style>
<body>
<div class='number_container'>
<span class='count '>317249</span>
<input type="hidden" class="currency" id="hiddenInput"></input>
</div>
</body>
</html>
相关文章:
- D3 - 如何从 xAxis 中删除逗号分隔符
- 在逗号和句号后添加空格,但如果是十进制分隔符,则不添加空格
- 如何将带有逗号千位分隔符的字符串解析为数字
- 分析不带逗号分隔符的 JSON 数据
- 简单购物车不允许使用千个逗号分隔符
- 在JavaScript中使用逗号-十进制分隔符分析数字
- Jquery自动完成:如何忽略逗号作为分隔符
- Chosen Jquery:将多个选定值的分隔符改为管道,而不是逗号
- 如何对由分隔的数字使用jquery函数.(点)或,(逗号)作为千分隔符
- 带逗号分隔符的数字
- “Only"数量;带逗号分隔符的字段
- 重写split()逗号分隔符
- 在我的计算器脚本中四舍五入值并为该值添加逗号分隔符
- 在javascript中给分隔符/逗号(,)一个别名
- JavaScript Join在数组中只有一个项目时插入逗号分隔符
- 在输入类型number中添加千个逗号分隔符
- 将字符串与点或逗号作为十进制分隔符转换为JavaScript中的数字
- 如何将分隔符从逗号更改为管道?
- 将Dojo NumberTextBox分隔符逗号替换为连字符
- 尝试为数字添加分隔符(逗号's和.)