Search for HTML element tags Based on ID (search for child elements and find parent elements)

Source: Internet
Author: User
Find the specific elements of the context based on the current node
Search for child elements:
Code transferred from birdhome
<SCRIPT Lanuage = "JavaScript">
Function findchildelement (element, tagname)
{
VaR isfounded = false;
VaR elements = element;
VaR result = element;
If (element. tagname = tagname)
{
Return element;
}
While (! Isfounded & elements! = NULL & result! = NULL & result. tagname! = Tagname)
{
Elements = elements. childnodes;
For (VAR I = 0; elements! = NULL & I <elements. length; I ++)
{
Result = elements. item (I );
VaR result2 = findchildelement (result, tagname );
If (result = NULL | result2 = NULL)
{
Continue;
}
If (result. tagname = tagname | result2.tagname = tagname)
{
If (result2.tagname = tagname)
{
Result = result2;
}
Isfounded = true;
Break;
}
}
}
If (isfounded)
{
Return result;
}
Else
{
Return NULL;
}
}
</SCRIPT>

Find parent Element
 

<SCRIPT Lanuage = "JavaScript">
Function findparentelement (element, tagname)
{
While (element! = NULL & element. tagname! = Tagname)
{
Element = element. parentelement;
}
If (element! = NULL & element. tagname = tagname)
{
Return element;
}
Return NULL;
}
</SCRIPT>

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.