本文介绍了parseInt函数()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 所以我有一个像这样的表格设置: < table> < tr> < td> 425< / td> < / tr> < / table> 和我是使用以下代码从表中检索值: var x = document.getElementById(''myTable'')。行 var y = x [0] .cells var usedMin = y [0] .innerHTML; 现在,我知道当我打电话时,usedMin是一个数字: 返回usedMin 我得到425。 (我希望得到的数字。)但是出于某种原因,如果我想要b / b 尝试对它做任何数学计算,例如: usedMin - 100 parseInt(usedMin) 我收到NaN错误。有谁知道为什么我不能输入这个 整数? -benjamin 解决方案 在你所显示的代码中,你还没有使用parseInt()以一种方式启用 你用usedMin进行计算。 - Joakim Braun 请不要浪费时间发布代码''有点类似 就像代码的一般概念那样不适合你。 你很可能会得到回应指出你没有设置 表格的id属性和usedMin - 100是一个无操作。 相反,构建一个最简单的测试用例来显示你的问题。 这样做你经常解决自己的问题,如果你不愿意,那么你可能会更愿意为那些愿意帮助你的人提供帮助。在此期间,请看一下这段代码,这个代码可以在Firefox和IE中运行,看看与你的代码有什么不同。 我敢打赌 除了425之外,你在单元格中有一些HTML标记: < html> < head> < script type =" text / javascript"> function foo(){ var x = document.getElementById(''myTable'')。rows var y = x [0] .cells var usedMin = y [0] .innerHTML; usedMin- = 100; alert(parseInt(usedMin)); } < / script> < / head> < body> < table id =" myTable"> < tr> < td> 425< / td> < / tr> < / table> < button onclick =" foo()"> foo< / button> < / body> < / html> 请不要浪费我们的时间来发布那些有点像的代码 你可能会得到回应,指出你没有设置 id你的表格的属性和usedMin - 100是一个无操作。 相反,构建一个最简单的测试用例来显示你的问题。你会经常在这样做的时候解决你自己的问题,如果你这样做了不,你会让那些愿意帮助你的人更容易。在此期间,看一下这段代码,它可以在Firefox和IE中运行,看看与你的代码有什么不同。我打赌你有一些HTML标记在单元格中除了425之外: < html> < head> < script type =" text / javascript" > 函数foo(){ var x = document.getElementById(''myTable'')。行 var y = x [0] .cells var usedMin = y [0] .innerHTML; usedMin- = 100; alert(parseInt(usedMin)); 我忘了指出减去 100之后使用parseInt()完全是浪费,因为减法将字符串 转换为数值。将数值传递给parseInt()强制 将值转换回字符串,然后解析整数 值。 So I have a table setup like this: <table><tr><td>425</td></tr></table> and I''m retrieving a value out of a table using this code: var x=document.getElementById(''myTable'').rowsvar y=x[0].cellsvar usedMin = y[0].innerHTML; now, I know that usedMin is a number, when I call: return usedMin I get "425". (the number I expect to get.) But for some reason, if Itry to do any math on it, like: usedMin - 100parseInt(usedMin) I get the NaN error. Does anyone know why I can''t type cast this as aninteger? -benjamin 解决方案 In the code you''ve shown, you haven''t used parseInt() in a way that enablesyou to do calculations with usedMin. --Joakim Braun Please don''t waste our time by posting code that''s sorta kindalike the general idea of the code that''s not working for you. You''re likely to get responses pointing out that you haven''t setthe id attribute of your table and that "usedMin - 100" is a no-op. Instead, build the simplest test case that shows your problem.You''ll often solve your own problem while doing this, and ifyou don''t, you''ll make it easier for people who might be willingto help you. In the meantime, take a look at this code, whichworks in Firefox and IE and see what''s different from your code.I''m betting that you''ve got some HTML markup in the cell inaddition to "425": <html><head><script type="text/javascript">function foo() {var x=document.getElementById(''myTable'').rowsvar y=x[0].cellsvar usedMin = y[0].innerHTML;usedMin-=100;alert(parseInt(usedMin));}</script></head><body><table id="myTable"><tr><td>425</td></tr></table><button onclick="foo()">foo</button></body></html> Please don''t waste our time by posting code that''s sorta kindalike the general idea of the code that''s not working for you.You''re likely to get responses pointing out that you haven''t setthe id attribute of your table and that "usedMin - 100" is a no-op.Instead, build the simplest test case that shows your problem.You''ll often solve your own problem while doing this, and ifyou don''t, you''ll make it easier for people who might be willingto help you. In the meantime, take a look at this code, whichworks in Firefox and IE and see what''s different from your code.I''m betting that you''ve got some HTML markup in the cell inaddition to "425":<html><head><script type="text/javascript">function foo() { var x=document.getElementById(''myTable'').rows var y=x[0].cells var usedMin = y[0].innerHTML; usedMin-=100; alert(parseInt(usedMin)); And I forgot to point out that using parseInt() after subtracting100 is a complete waste, since the subtraction converts the stringto a numeric value. Passing a numeric value to parseInt() forcesit to cast the value back into a string, then parse out the integervalue. 这篇关于parseInt函数()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-25 08:28