javascript, dom - 为什么选择的子节点每 2 编号
我有一个奇怪的问题。我想遍历选择的子节点。简单吧?这是我的 html:
<select multiple="multiple" id="selectBoxOne" size="5" class="selectListBox">
<option value="0" id="multiple0" {MULTIPSEL0}>0</option>
<option value="1" id="multiple1" {MULTIPSEL1}>1</option>
<option value="2" id="multiple2" {MULTIPSEL2}>2</option>
<option value="3" id="multiple3" {MULTIPSEL3}>3</option>
<option value="4" id="multiple4" {MULTIPSEL4}>4</option>
<option value="5" id="multiple5" {MULTIPSEL5}>5</option>
</select>
dom 看起来像这样:
所以问题是我在真实节点之间有空节点。真实节点是 1,3,5,7,9,11,而不是 1,2,3,4,5。所以如果我使用类似的东西:
alert(document.getElementById('selectBoxOne').childNodes[2].innerHTML);
我会得到未定义的结果。
我在很多地方使用这个脚本,所以我不能只读取奇数(因为其他地方是正常的,之间没有空孩子)。
知道为什么会发生这种情况或如何解决它吗?感谢您抽出时间。
注:{MULTIPSEL4} ->是一个模板变量。在这种情况下是空的。其他情况下是 select="selected" 。
Js:
for (i = 1; i <= optionNumber; i++)
{
selectOptions[i-1] = document.getElementById('selectBoxOne').childNodes[i].innerHTML;
}
alert(selectOptions);
结果:
I have an odd problem. I want to go through the child nodes of a select. Simple right? So this is my html:
<select multiple="multiple" id="selectBoxOne" size="5" class="selectListBox">
<option value="0" id="multiple0" {MULTIPSEL0}>0</option>
<option value="1" id="multiple1" {MULTIPSEL1}>1</option>
<option value="2" id="multiple2" {MULTIPSEL2}>2</option>
<option value="3" id="multiple3" {MULTIPSEL3}>3</option>
<option value="4" id="multiple4" {MULTIPSEL4}>4</option>
<option value="5" id="multiple5" {MULTIPSEL5}>5</option>
</select>
The thing is that dom looks like this:
So the problem is that I have that empty nodes between the real nodes. The real nodes are 1,3,5,7,9,11 instead of 1,2,3,4,5. So if I usesomething like:
alert(document.getElementById('selectBoxOne').childNodes[2].innerHTML);
I get undefined.
I use this script in many places so I can't read only the odd numbers (since the other places are normal, without empty childs between).
Any ideea why this happened or how to fix it? Thank your for your time.
Note: {MULTIPSEL4} -> is a template variable. Is empy in this case. Is select="selected" in other cases.
Js:
for (i = 1; i <= optionNumber; i++)
{
selectOptions[i-1] = document.getElementById('selectBoxOne').childNodes[i].innerHTML;
}
alert(selectOptions);
Result:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选项元素之间有空格。您的解析器正在为每个节点生成一个文本节点。
如果您有支持 HTML 的 DOM API,请在通用 DOM 或
.options
中使用getElementsByTagName('option')
而不是.childNodes
。You have white space between the option elements. Your parser is generating a text node for each one.
Use
getElementsByTagName('option')
instead of.childNodes
in a generic DOM or.options
if you have an HTML aware DOM API.你的做法是错误的。
迭代下拉选项的正确方法是:
这些文本节点之所以存在,是因为在 HTML 源代码中
option
元素之间有换行符和空格字符。您可以通过以下方式避免这种情况:您不必这样做,因为您有更好的方法来迭代项目。
You are doing it the wrong way.
The correct way to iterate over drop down options is:
Those text nodes are there because in the HTML source you have newline and blank space characters between the
option
elements. You can avoid this by:By you don't have to because you have better way to iterate the items.