如何检查 value 是否为数字。如果它是数字值,则显示文本居中,否则显示文本左侧。我想知道我的说法有什么问题。
for (var l = 0; l < Object.keys(pods[i].data.summaryDetailedData).length; l++) {
contentHtml += "<tr>";
for (var m = 0; m < pods[i].data.columns.length; m++) {
for (var field in pods[i].data.summaryDetailedData[l]) {
var rowspan = (pods[i].data.summaryDetailedData[l].children !== undefined ? pods[i].data.summaryDetailedData[l].children.length : "");
rowspanMax = Math.max(rowspanMax, rowspan);
if (field === pods[i].data.columns[m]) {
var preFix = pods[i].data.summaryDetailedData[l]["sPrefix"] !== undefined ? pods[i].data.summaryDetailedData[l]["sPrefix"] : "";
var postFix = pods[i].data.summaryDetailedData[l]["sPostfix"] !== undefined ? pods[i].data.summaryDetailedData[l]["sPostfix"] : "";
var value = pods[i].data.summaryDetailedData[l][field];
if (value.toString().substr(0, 3) == "- 1") {
value = "N/A";
}
var color = pods[i].data.summaryDetailedData[l][field + "_color"];
if (colName[m] === "sLabel" && pods[i].data.summaryDetailedData[l].bStepThrough == true) {
value = "<a href=\"#\" class=\"stepthrough\">" + value + "</a>";
}
color = color !== "" && color !== undefined ? " <span class=\"color\" style=\"background: #" + color + "\"></span>" : " <span class=\"color\"></span>";
contentHtml += "<td rowspan1=\"" + 1 + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || typeof value == Number) ? "text-center" : "text-left") + "\">" + value + (Number(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
if (rowspan > 1) {
var rowspanContent = "<td rowspa1=\"" + rowspan + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || typeof value == Number) ? "text-center" : "text-left") + "\">" + value + (Number(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
}
if (field === "sLabel") {
for (var child in pods[i].data.summaryDetailedData[l].children) {
if (child > 0 && rowspan >= 2) {
contentHtml += "</tr>";
contentHtml += "<tr>";
contentHtml += rowspanContent;
}
最佳答案
您使用typeof
:
var ff=1.222; console.log(typeof ff) // number
(typeof ff == "number") // true
var ff="1.222"; console.log(typeof ff) // string
(typeof ff == "number") // false
或
使用toFixed
方法,如果变量不是数字,则返回undefined
,否则,数字对象将在其原型(prototype)中定义函数,因此它将返回函数 toFixed
本身,因此它是一个数字。
(this is sort of a hack, but it is efficient and least cost)
contentHtml += "<td class=\""
+ (value.toFixed) ? "text-center" : "text-left" + "\">" )
+ value + (value.toFixed) ? preFix : ""
+ color + (value.toFixed) ? postFix : "" + "</td>";
关于javascript - 如何检查值是否是数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41054514/