解析 XML NaN 错误?

发布于 2024-12-01 02:39:19 字数 817 浏览 1 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的鱼塘能养鲲 2024-12-08 02:39:19
+'</td>'+'</tr>'+

删除本行末尾的+(下一行也以+开头)

+'</td>'+'</tr>'+

remove the + at the and of this line(the next line starts with a + too)

风筝有风,海豚有海 2024-12-08 02:39:19

(这不是一个不回答,而是更多的评论,但是我需要更多的空间和答案的格式来表明我的观点。)

避免这种错误的最佳方法是始终使用关于如何格式化你的代码的代码约定 。代码。有许多关于代码约定的建议,但这并不重要您使用哪一种,只要您觉得舒服并且最重要的是始终如一地使用它。

在您的情况下,如果您将长表达式包装在多行中,则应该注意三件事:

  • 识别以下行
  • 在二元运算符之前和之后使用空格
  • 最重要的是,将运算符一致地放置在开头或结尾行尾。 (不要混合放置,否则你会得到像你一样的错误。)

就我个人而言,我喜欢将操作符放在行的末尾,这样你“知道”该行必须继续,并且有更少的将二进制 + 与一元 + 混淆的危险,就像您的情况一样。

'<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>'

(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:

  • Ident the following lines
  • Use spaces before and after a binary operator
  • And most importantly place the operators consistently either at the start or the end of the line. (Don't go mixing the placement, or you'll get errors like you did.)

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.

'<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>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文