全部。我正在尝试将字符串更改为 float ,以便可以将其格式化为货币。问题是,parseFloat 不断返回 NaN。我缺少什么?这是 JSFiddle 。
HTML:
<div class="value num">948572</div>
<div class="value num">34567</div>
<div class="value num"></div>
JS:
jQuery(document).ready(function($) {
$('.value.num').each(function() {
if( $(this).is(':empty')) {
$(this).text('$0.00');
} else {
var numValue = Number.parseFloat($(this), 10);
$(this).text('$' + (numValue / 100).toFixed(2));
}
});
});
最佳答案
如果您想使用 jQuery 获取 html 元素的文本,请使用 .text()
jQuery(document).ready(function($) {
$('.value.num').each(function() {
if( $(this).is(':empty')) {
$(this).text('$0.00');
} else {
var numValue = Number.parseFloat($(this).text(), 10);
$(this).text('$' + (numValue / 100).toFixed(2));
}
});
});
关于javascript - jQuery parseFloat 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35446286/