棘手的Javascript循环 [英] Tricky Javascript Looping

查看:80
本文介绍了棘手的Javascript循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要显示的广播电台的XML列表,但我认为它涉及一些我无法弄清楚的嵌套FOR循环。


这是我的XML示例:

I''ve got an XML list of radio stations that I want to display, but I think it involves some nested FOR loops that I can''t quite figure out.

Here''s a sample of my XML:

展开 | 选择 | Wrap | 行号

推荐答案

您遇到的一个问题是< I>的childNodes 的。请注意,子节点不仅是一个元素,甚至元素之间的空格也算作子节点!你最好用 getElementsByTagName()


现在主要问题是, x y 代表独立的 NodeLists(非常简单的数组),所以请注意你需要索引的地方(以及哪个变量去那里)。


如果你回报看不出我的意思。


PS:使用FireBug会对你有很大帮助


旁注:建议用使用DOM方法(或 .innerHTML )显示结果,而不是通过 document.write()
one problem you have is using childNodes. mind that a child node is not only an element, even whitespace between elements counts as child node! you?re better off with getElementsByTagName().

now to the main problem, x and y represent independent NodeLists (very simple arrays), so take care where you need an index (and which variable goes there).

report back if you can?t see what I mean.

PS: using FireBug will help you a great deal here

side note: it is recommended to display the results using DOM methods (or .innerHTML) rather than through document.write()


感谢您的回复。


我可以看到如何使用getElementsByTagName()直接访问< station>节点而不是使用childNodes,但我不知道我将如何显示每个州的那些节点。那仍然会使用某种嵌套的FOR循环,或者可能是IF语句吗?我想这就是我滥用x和y变量导致我误入歧途的地方。


我确实切换到使用innerHTML,至少只列出状态:
Hi, thanks for the response.

I can see how using getElementsByTagName() would directly access the <station> nodes instead of using childNodes, but I don''t see how I would display just the ones that go with each state. Would that still use some sort of nested FOR loops, or maybe IF statements? I guess that''s where my misuse of the x and y variables is leading me astray.

I did switch to using innerHTML, at least to just list the states:
展开 | 选择 | Wrap | 行号


@npm


访问< state> s就像访问<站GT; S。从一般的角度来看,它们是相同的。

@nn


您的原始代码在修复 childNodes后正常工作 x y 错误。是的,你需要嵌套循环(因为你有嵌套数据(因为有办法解决这个问题,但我不认为那更好吗?))

@npm

尝试脚本选项卡,在那里你可以设置断点(通过点击行号之前和Firebug将在下一次运行中停止,这样你就可以检查每个变量(例如见为什么跳过循环)
@npm
access the <state>s like you access the <station>s. from a general point of view, they are the same.

@npm
your original code worked all right after fixing the childNodes, x and y bugs. and yes, you need nested loops (because you have nested data (of cause there are ways to work around that, but I don?t consider that better? yet))

@npm
try out the script tab, there you can set breakpoints (by clicking just before a line number and Firebug will stop there in the next run so you can inspect each of the variables (and for instance see why a loop is skipped)


这篇关于棘手的Javascript循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆