使用getElementsByTagName()返回的元素的顺序 [英] Order of the elements returned using getElementsByTagName ()

查看:106
本文介绍了使用getElementsByTagName()返回的元素的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 getElementsByTagName 读取控件是读取元素的常见做法。但是我想知道我们是否可以信任使用此函数返回元素的顺序。

Reading the controls using getElementsByTagName is very common practice to read the element. However I would like to know that whether we can trust the order in which the elements are returned using this function.

它是否按照它们在UI上的顺序返回元素?或者它也可以返回随机元素,在其他时间我们不能信任序列?

Does it return elements in the order in which they are placed on the UI? Or it can return random elements too where we can't trust on the sequence at other times?

var labels = document.getElementsByTagName("label"), i;
for(i=0;i<labels.length;i++)
{
    if(i == 1)
        labels[i].innerText = "First Value";
    else if (i==2)
        labels[i].innerText = "Second Value";

    if (labels[i].innerText  == "NULL") {
        labels[i].innerText  = "Empty";
    }
}


推荐答案

这函数总是以相同的深度优先顺序返回元素。

这是它们在HTML树结构中出现的顺序。

This function always return elements in the same depth-first order.
This is the order, in which they appear in HTML tree structure.

这篇关于使用getElementsByTagName()返回的元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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