Jquery将变量显示为html div

时间:2013-04-22 08:29:28

标签: javascript jquery divide

我定义了以下变量:

var TuesdaysRatio = myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal;

console.log(TuesdaysRatio);包含Chrome中的输出:0.032636762568610575。我脚本中的下一行是:

$('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');

我正在使用JQuery将段落中的变量显示在脚本标记上方创建的空div(类ratio-div)中。在我的网页上而不是Tuesdays Likes per Organic Impressions Ratio 0.032636762568610575我有Tuesdays Likes per Organic Impressions Ratio NaN,我无法弄明白问题在哪里...

4 个答案:

答案 0 :(得分:0)

试试这个

$(document).ready(function(){
     var TuesdaysRatio = parseFloat(parseInt(myTuesdaysNewLikesTotal) / parseInt(myTuesdaysImpressionsOrganicTotal));
     $('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');
});

答案 1 :(得分:0)

首先尝试解析您的值:

var TuesdaysRatio = parseInt((myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal), 10);

答案 2 :(得分:0)

编号..使用Number() .. parseIntparseFloat可能会更改您的结果,因此请转到Number()函数,该函数将object参数转换为表示对象的价值。

var TuesdaysRatio = Number(myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal);

答案 3 :(得分:0)

$('document').ready(function(){
    var TuesdaysRatio = Number(0.032636762568610575);
    $('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');
});

试试这个