事业部生产NaN [英] Division produces NaN
本文介绍了事业部生产NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将变量 number
除以 100
并得到 NaN
。我是jQuery的新手,有人可以帮助我了解我在做什么吗?
I'm trying to divide the variable number
by 100
and get NaN
. I'm new to jQuery, can anyone help me to understand what I'm doing wrong?
$.each(data.products, function (i, data) {
var div_data = "<p>" + data.title + "<br/>" + data.description + "</p>";
div_data += '<img src="' + data.imageUrl + '" title="Loading..." alt="Loading...">';
div_data += '<p><a href="' + data.url + '">Buy</a></p>';
$(div_data).appendTo("#testjson");
number = '<div class="number">' + data.price + '</div>';
$('#myElement').text((number / 100));
});
推荐答案
首先要设置数字
到字符串:
number = '<div class="number">'+ data.price + '</div>';
然后,您将字符串除以 100
Then you are dividing that string by 100
在JavaScript中导致 NaN
,是 Not a Number
的缩写。在规范中指定。
In JavaScript that results in NaN
which is short for Not a Number
. This is specified in the spec.
可能的解决方案是将数字本身除以100,而不是HTML字符串,例如:
A possible solution could be to divide the number itself by 100, and not the HTML string, for example:
var number = data.price/100;
$('#myElement').text(number);
这不会将嵌套的div附加到您的 #myElement
。
This would not attach a nested div to your #myElement
though.
这篇关于事业部生产NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文