Traverse each element in SVG

Source: Internet
Author: User
In SVG, to traverse all the elements, we must implement it using JavaScript (JS). Hey, I didn't say this. Anyone who wants to learn SVG knows that we also need some Dom interfaces. The following describes Code All are JS Code.

First, pass the document.doc umentelement attribute to obtain the whole SVG document, that is, the root element of SVG. All nodes are included in this element. Of course, if you want to access each subnode in a node with a known ID, you can directly find the local root element through document. getelementbyid ("element ID. For coherence, assume that node represents the root node of each element to be accessed. Next, you can access the nodes in the root element. You can use the for statement to access each node in sequence. The initial value is 0 and the final value is node. childnodes. length. In SVG, the blank area between two node elements is also an element, and its nodetype value is 3, that is, text_node. Therefore, this feature can be used to exclude it. You can access each of the other nodes through node. childnodes. Item (I.
VaR svgmainmapdoc = id2.getsvgdocument ();

VaR node1_svgmainmapdoc.doc umentelement;
VaR child;
For (VAR I = 0; I <node. childnodes. length; I ++ ){
If (node. childnodes. Item (I). nodetype! = 3 ){
Child = node. childnodes. item (I );
If (child. getattribute ("ID "))
{
Alert (child. getattribute ("ID "))
}
}
}
Id2 is
<Embed src = "Unnamed-1.svg" type =" image/SVG + XML "name =" ID2 "pluginspage =" http://www.adobe.com/svg/viewer/install/main.html"
Menu = "false" width = "30" Height = "30"> </embed>

The following is a recursive traversal of all parent and child nodes.

Function getlayer ()
{
VaR svgmainmapdoc = id2.getsvgdocument ();

VaR node1_svgmainmapdoc.doc umentelement;

Findnode (node)
}

// Recursively traverse the node nodetype. The blank area between the three node elements is also an element, and the 4 node is a script.
Function findnode (node)
{
For (VAR I = 0; I <node. childnodes. length; I ++)
{
If (node. childnodes. Item (I). nodetype! = 3 & node. childnodes. Item (I). nodetype! = 4)
{
VaR child = node. childnodes. item (I );
If (child. getattribute ("layer "))
{
Alert (child. getattribute ("layer "));
}
If (child. childnodes)
{
Findnode (child );
}
Else
{
Break;
}
}
}
}

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.