JS How to get a child label for an ID

Source: Internet
Author: User

Idea: Get the parent object by ID, and then use ChildNodes to get all the child object arrays, the key code:

document.getElementById (div_id). ChildNodes; Array of sub-objects

Example Demo: Click the button to add a red class for all the child tags of the div tag with ID test, that is, the font is displayed

1. HTML structure

<div id = "TEST" >
<a href= "#" > I am a hyperlink </a>
<input type= "text" value= "I am text box" >
<div> I am a son div</div>
</div>
<input type= ' button ' value= ' Set child element style ' onclick= ' fun () '/>

2. CSS style

. red{color:red!important;}

3. JavaScript code

function Fun () {
OBJS = document.getElementById ("Test"). ChildNodes;
For (k in Objs)
Objs[k].classname = "Red";
}

4, before and after the comparison effect is as follows

JS How to get a child label for an ID

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.