javascript jquery显示变量成html div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16142586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery display variable into html div
提问by Daniela costina Vaduva
I have defined the following variable:
我定义了以下变量:
var TuesdaysRatio = myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal;
The console.log(TuesdaysRatio);
is has the output in Chrome: 0.032636762568610575
. The next line in my script is the following:
本console.log(TuesdaysRatio);
就是在Chrome浏览器中输出:0.032636762568610575
。我的脚本中的下一行如下:
$('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');
I am using JQuery to display the variable in a paragraph into an empty div (with the class ratio-div
) created above the script tag. On my page instead of Tuesdays Likes per Organic Impressions Ratio 0.032636762568610575
I have Tuesdays Likes per Organic Impressions Ratio NaN
and I can't figure it out where is the issue...
我正在使用 JQuery 将段落中的变量显示到ratio-div
在脚本标记上方创建的空 div(带有 class )中。在我的页面上而不是Tuesdays Likes per Organic Impressions Ratio 0.032636762568610575
我有Tuesdays Likes per Organic Impressions Ratio NaN
,我无法弄清楚问题出在哪里......
采纳答案by Prasath K
Number it.. Use Number().. parseInt
and parseFloat
may change your result so go for Number()
function which converts the object argument to a number that represents the object's value.
编号.. 使用Number()..parseInt
并且parseFloat
可能会改变你的结果所以去Number()
函数将对象参数转换为代表对象值的数字。
var TuesdaysRatio = Number(myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal);
回答by Rohan Kumar
Try this
试试这个
$(document).ready(function(){
var TuesdaysRatio = parseFloat(parseInt(myTuesdaysNewLikesTotal) / parseInt(myTuesdaysImpressionsOrganicTotal));
$('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');
});
回答by Eli
Try to parse your value first:
首先尝试解析您的值:
var TuesdaysRatio = parseInt((myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal), 10);
回答by diganta
$('document').ready(function(){
var TuesdaysRatio = Number(0.032636762568610575);
$('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');
});
Try this
试试这个