简体   繁体   English

如何在JavaScript中访问嵌套的html-child元素

[英]How to access nested html-child elements in javascript

I'm using a html-form with nested div-elements with this structure: 我正在使用具有此结构的嵌套div元素的html表单:

<form id="form">
    <div id="parentproperties">
        <legend class="itemName">People</legend>
        <br>
        <div id="properties1">
            <legend class="itemName">Name</legend>
            <label class="valueId">JaneDoe</label>
            <br>
            <label class="labelId">Id: </label>
            <input class="inputId">5646543</input>
        </div>
        <div id="properties2">
            <legend class="itemName">Address</legend>
            <label class"valueId">MysteriousStreet</label>
            <br>
            <label class="labelId">Id: </label>
            <input class="inputId">1234</input>
        </div>
        <div id="properties3">
            <legend class="itemName">Country</legend>
            <label class"valueId">SomeCountry</label>
            <br>
            <label class="labelId">Id: </label>
            <input class="inputId">7899542</input>
                    <div id="properties4">
                    <legend class="itemName"></legend>
                    <br>
                    </div>
        </div>
    </div>
</form>

Now I need to access the field with the Id 'valueID' for every 'div' to change a specific value. 现在,我需要为每个“ div”访问带有ID“ valueID”的字段,以更改特定值。 I've tried several loops but they do not not work properly... Later I've tried to get a particular element directly this way: 我尝试了几个循环,但是它们无法正常工作……后来,我尝试通过这种方式直接获取特定元素:

document.getElementById("properties").childNodes[0].innerHTML;

...but I only get the first valueId with value 'JaneDoe'. ...但是我只得到第一个valueId和'JaneDoe'值。 Thanks in advance for any ideas! 预先感谢您的任何想法!

As mentioned in the comments, the current HTML structure needs some attention: 如评论中所述,当前的HTML结构需要引起注意:

  1. Don't use a unique ID more than once. 请勿多次使用唯一ID。 Use classes, data- attributes, or unique IDs. 使用类, data-属性或唯一ID。

  2. Input elements do not have closing tags, they're self-closing <input /> . 输入元素没有结束标签,它们是自动关闭的<input />

Now, to select the elements you want, you have several options. 现在,要选择所需的元素,您有几个选择。

  1. Give each element a unique ID and select it with 给每个元素一个唯一的ID,然后选择

     document.getElementById('input-1').value('new value'); 
  2. Or loop through the input elements with something like: 或使用类似以下内容的循环遍历input元素:

     document.querySelectorAll('.inputId')) 

    which will return a NodeList of all the elements with a class of inputId 这将返回带有inputId类的所有元素的NodeList。

 document.getElementById('input-1').value = "New Value" 
 <form id="form"> <div> <legend id="itemName">People</legend> <br> <div class="properties"> <legend class="itemName">Name</legend> <label class="valueId">JaneDoe</label> <br> <label class="labelId">Id: </label> <input class="inputId" id="input-1" value="5646543" /> </div> <div class="properties"> <legend class="itemName">Address</legend> <label class="valueId">MysteriousStreet</label> <br> <label class="labelId">Id: </label> <input class="inputId" id="input-2" value="1234" /> </div> <div class="properties"> <legend class="itemName">Country</legend> <label class="valueId">SomeCountry</label> <br> <label class="labelId">Id: </label> <input class="inputId" id="input-2" value="7899542" /> </div> <div id="properties"> <legend id="itemName"></legend> <br> </div> </div> </form> 

childNodes[0] selects the first node of all child nodes, so you can use childNodes[1] , childNodes[2] and so on. childNodes[0]选择所有子节点中的第一个节点,因此可以使用childNodes[1]childNodes[2]等。 As Teemu said, id s should be unique, in your case it looks like a good point to use classes. 正如Teemu所说, id应当是唯一的,在您的情况下,使用类似乎是一个不错的方法。

as @Teemu says in comment id s should be unique within the document, instead you could give them some class="a" 正如@Teemu在评论id所说,该id在文档中应该是唯一的,相反,您可以给他们一些class="a"

then you could get all elements by class document.getElementsByClassName("a") 那么您可以通过document.getElementsByClassName("a")类获取所有元素

Use children instead of childNodes : 使用子项而不是childNodes:

document.getElementById("properties1").children[0].innerHTML = 

Also you should use unique id string for element id. 另外,您应该使用唯一的ID字符串作为元素ID。

childNodes return a NodeList object which also includes textNodes and commentNodes etc. But, children method will only give you HTMLCollection object . childNodes返回一个NodeList对象 ,该对象还包括textNodes和commentNodes等。但是,children方法只会给您HTMLCollection对象

使用类属性代替id属性很简单。

 document.getElementsByClassName("properties")[0].childNotes[0].innerHTML; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM