在数学上使用inner.HTML吗? [英] Use an inner.HTML mathematically?
问题描述
我正在编写一个脚本,在该脚本中,我必须使用一个为数字的inner.HTML,并对其进行数学处理.这是示例:
I'm making a script, in which i have to use an inner.HTML which is a number, and work with it mathematically. Here's the example:
<span id="searchResults">2301</span>
如您所见,inner.HTML是一个数字,我想编写一个像这样的脚本:
As you can see, the inner.HTML is a number, and I'd like to make a script like:
var results = document.getElementById("searchResults");
if (results > 3000)
{
location.reload(true);
}
当然不可能,因为脚本不会将inner.HTML视为可以在数学上使用的数字.
Of course this isn't possible because the script doesn't see the inner.HTML as a number it can mathematically work with.
那么,有没有一种方法可以将inner.HTML转换成我可以用来做数学运算的数字?
So, is there a way to convert the inner.HTML into a number I can do math with?
感谢您的帮助!
推荐答案
您可以使用一元运算符+
将字符串转换为数字,例如
You can use unary operator +
for convert string to number something like
var results = +(document.getElementById("searchResults").innerHTML);
//------------^
您还忘记了使用.innerHTML
这篇关于在数学上使用inner.HTML吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!