Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用document.write()更改整数值_Javascript_Xml - Fatal编程技术网

Javascript 使用document.write()更改整数值

Javascript 使用document.write()更改整数值,javascript,xml,Javascript,Xml,我有以下XML: <beanRepresentation> <beanRepId>222</beanRepId> <beanRepName>Bean 1</beanRepName> <topY>10</topY> <leftX>10</leftX> <height>10</height> <widt

我有以下XML:

 <beanRepresentation>
     <beanRepId>222</beanRepId>
     <beanRepName>Bean 1</beanRepName>
     <topY>10</topY>
     <leftX>10</leftX>
     <height>10</height>
     <width>10</width>
 </beanRepresentation>
这会给我10分,但我想要20分,我试着

document.write(x[i].getElementsByTagName("topY")[0].childNodes[0].nodeValue +10);

但它不起作用,它给了我1010。我该怎么处理这个数字呢

节点值返回的值是一个字符串(例如,请参见),因此您执行的是字符串连接而不是加法。在执行以下操作之前,应将结果转换为数字:

document.write( parseInt( x[i].getElementsByTagName("topY")[0].childNodes[0].nodeValue, 10)  + 10);

无论如何,我建议你阅读这个问题的答案:

你说的“它不起作用”是什么意思?它实际上起了什么作用?我怀疑你得到了类似于
1010
的结果,因为nodeValue是一个字符串,而不是一个数字。Javascript发现,
nodeValue
是一个字符串,所以它将数字转换成一个字符串,并将它们混合在一起。要获得真正的数学,您必须将
nodeValue
转换为一个数字。明白了,谢谢您的建议。谢谢您的解决方案!
document.write( parseInt( x[i].getElementsByTagName("topY")[0].childNodes[0].nodeValue, 10)  + 10);