Home >
Article > Web Front-end > Introduction to SVG 2D in HTML5 12—Introduction to SVG DOM and DOM operations_html5 tutorial skills
Introduction to SVG 2D in HTML5 12—Introduction to SVG DOM and DOM operations_html5 tutorial skills
WBOYOriginal
2016-05-16 15:50:071534browse
Using scripts can easily complete various complex tasks, and it is also a mainstream way to complete animation and interaction. Since SVG is an element of html, it supports ordinary DOM operations. And since SVG is essentially an xml document, there is also a special DOM operation, which is mostly called SVG DOM. Of course, since IE currently does not support SVG, developing SVG pages based on IE requires different methods. Everyone is actually familiar with this part of knowledge, so let’s just take a brief look at it.
DOM operations in HTML pages Everyone should be familiar with DOM. Here is a small example:
Have you noticed, the DOM operation of ordinary html elements is exactly the same: Select element: document.getElementById Create element: document.createElementNS Another way to create child elements: element. createChildNS Add an element: node.appendChild Set the element's attributes: element.setAttributeNS/element.setAttribute In addition to the above operations, the following operations and attributes are also common: Get the element's attributes Attribute value: element.getAttributeNS/element.getAttribute Check whether an attribute exists on the element: element.hasAttributeNS Remove an attribute of the element: element.removeAttributeNS Parent element, child element and sibling node: element. parentNode/element.firstChild/child.nextSibling These methods will not be introduced in detail here; in addition, the node structure of the DOM tree and the inheritance relationship between objects are also similar, so they will not be described in detail. Students who need it can refer to the documentation of DOM Core Object later. However, it should be noted that SVG is essentially an XML document, so the basic DOM methods used are all ending with NS to provide related namespaces; if a namespace has been provided when creating an element, and there are not multiple namespaces problem, then when setting related attributes, you can also choose to use the version without NS, such as directly using element.setAttribute to set the attribute value, but in general, it is still strongly recommended to use the version with NS ending, because this version always It works fine, even in the case of multiple namespaces. SVG DOM How is this different from the standard DOM? I haven’t found any comprehensive information. Currently, I only know that the methods of assigning attributes are different. If there are any students who know about this, please let me know. In the above example, we use element.setAttributeNS/element.setAttribute to assign values to attributes. In SVG DOM, you can use the object-oriented method to assign values to the attributes of objects by accessing dot numbers. For example, the following are two Comparison of methods: Common DOM method:
DOM script is a traditional script, which is characterized by passing Construct "value strings" to set individual items. The advantage of the SVG DOM script style is that you don't have to build a "value string", so the performance is better than DOM script.
Script embedded in SVG If you want to add a script inside SVG, you need to use the script element. This has been mentioned before. Apart from this, it is basically the same as putting the script in It's the same into the external HTML. Look at an example:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn