解析 XML NaN 错误?
我用 JS 得到了这段代码,从 XML 解析,我的输出在 Chrome、FF、IE 中显示 NaN……我不知道为什么,也不知道它来自哪里。我所有的 XML 字段都是字符,这就是我使用 .text() 函数的原因...
function parse(document){
$(document).find("EMaDetails").each(function(){
$("#main").append(
'<table>'
+'<tr>'+'<td>'
+$(this).find('Nachn').text()+', '
+$(this).find('Vorna').text()
+'</td>'+'</tr>'+
+'<tr>'+'<td>'
+$(this).find('Detail1').text()+', '
+$(this).find('Detail2').text()
+'</td>'+'</tr>'
+'</table>'
);
});
}
结果如下:
NaN
Lastname1, Firstname1
Detail1, Detaila1
NaN
Lastname2, Firstname2
Detail2, Detaila2
谢谢
i got this code with JS, parsing from XML, my Output displays NaN in Chrome, FF, IE... and I don't know why, wether where it comes from. All my XML fields are Chars, thats why i use .text() function...
function parse(document){
$(document).find("EMaDetails").each(function(){
$("#main").append(
'<table>'
+'<tr>'+'<td>'
+$(this).find('Nachn').text()+', '
+$(this).find('Vorna').text()
+'</td>'+'</tr>'+
+'<tr>'+'<td>'
+$(this).find('Detail1').text()+', '
+$(this).find('Detail2').text()
+'</td>'+'</tr>'
+'</table>'
);
});
}
And the Result is like this:
NaN
Lastname1, Firstname1
Detail1, Detaila1
NaN
Lastname2, Firstname2
Detail2, Detaila2
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除本行末尾的+(下一行也以+开头)
remove the + at the and of this line(the next line starts with a + too)
(这不是一个不回答,而是更多的评论,但是我需要更多的空间和答案的格式来表明我的观点。)
避免这种错误的最佳方法是始终使用关于如何格式化你的代码的代码约定 。代码。有许多关于代码约定的建议,但这并不重要您使用哪一种,只要您觉得舒服并且最重要的是始终如一地使用它。
在您的情况下,如果您将长表达式包装在多行中,则应该注意三件事:
就我个人而言,我喜欢将操作符放在行的末尾,这样你“知道”该行必须继续,并且有更少的将二进制
+
与一元+
混淆的危险,就像您的情况一样。(This not an not answer but more of a comment, however I needed more space and the formatting of an answer to show my point.)
The best way to avoid this kind of error is to consistently use a code convention on how to format your code. There are many suggestions for code conventions around, but it's not important which one you use, as long you are comfortable with it and most importantly use it consistently.
In you case, where you are wrapping a long expression over several lines, there are three thing you should look out for:
Personnally I like to have the operator at the end of the line, so you "know" that the line has to continue, and there is less danger to confuse the binary
+
with a unary+
as it happened in your case.