使用javaScript和DOM操作svg元素

基本的svgDOM树:
 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
<g id="firstGroup">
<rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" />
<text x="40" y="100">This is a basic SVG document!</text>
</g>
</svg>
使用JAVASCRIPT访问SVG元素,并获取属性值
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300"> <script type="text/ecmascript"> <![CDATA[ function showRectColor() { alert(document.getElementById("myBlueRect").getAttributeNS(null,"fill")); }
 function showRectArea(evt)
{ var width = parseFloat(evt.target.getAttributeNS(null,"width"));
var height =parseFloat(evt.target.getAttributeNS(null,"height")); alert("The rectangle area is: " + (width * height)); }
function showRootChildrenNr()
{ alert("Nr of Children: "+document.documentElement.childNodes.length); } ]]> </script>
<g id="firstGroup">
<rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" onclick="showRectArea(evt)"/>
<text x="40" y="100" onclick="showRectColor()">Click on this text to show rectangle color.</text>
<text x="40" y="130">Click on rectangle to show rectangle area.</text> <text x="40" y="160" onclick="showRootChildrenNr()">Click on this text to show the number of child <tspan x="40" dy="20">elements of the root element.</tspan></text> </g> </svg>
设置单个元素的属性值
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
<script type="text/ecmascript">
<![CDATA[ function changeRectColor(evt) { var red = Math.round(Math.random() * 255); var green = Math.round(Math.random() * 255); var blue = Math.round(Math.random() * 255); evt.target.setAttributeNS(null,"fill","rgb("+ red +","+ green+","+blue+")"); } ]]> </script> <g id="firstGroup"> <rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" onclick="changeRectColor(evt)"/> <text x="40" y="100">Click on rectangle to change it's color.</text> </g> </svg>
检查,并删除属性值

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300"> <script type="text/ecmascript"><![CDATA[
function removeFill(evt) {
//通过获得元素,
var element = evt.target;
if (element.hasAttributeNS(null,"fill")) {
element.removeAttributeNS(null,"fill"); }
else { alert("This element doesn't have a fill attribute."); } } ]]></script>
 <g id="firstGroup"> <rect width="70" height="50" x="40" y="5" fill="blue" onclick="removeFill(evt)"/> <rect width="70" height="50" x="40" y="65" fill="blue" onclick="removeFill(evt)"/> <rect width="70" height="50" x="40" y="125" fill="blue" onclick="removeFill(evt)"/> <rect width="70" height="50" x="40" y="185" fill="blue" onclick="removeFill(evt)"/> <rect width="70" height="50" x="40" y="245" fill="blue" onclick="removeFill(evt)"/> <text x="150" y="30">Click on rectangle<tspan x="150" dy="15">to remove it's color.</tspan></text> </g> </svg>
父结点,子结点,和兄弟结点
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="100"> <script type="text/ecmascript">
<![CDATA[ function showContentAndRelatives(evt)
{
//get reference to text element
var textElement = document.getElementById("myText");
//get reference to parent of element
var parent = textElement.parentNode; alert("the parent node has id '"+parent.getAttributeNS(null,'id')+"'/nNodename is '" +parent.nodeName+"'");
//get a reference to the first child of the element "myText"
var child = textElement.firstChild; //loop over all childs
while (child != null) {
//see if child is a tspan and has child nodes
if (child.nodeName == "tspan" && child.hasChildNodes()) {
//see if firstChild is of nodeType "text" if (child.firstChild.nodeType == 3)
{ alert("child's text content="+child.firstChild.nodeValue); } }
child = child.nextSibling; } alert("the content of the second tspan Child is: "+textElement.childNodes.item(1).firstChild.nodeValue); } ]]></script>
<g id="firstGroup">
<text id="myText" x="50" y="30" onclick="showContentAndRelatives(evt)">
<tspan>Click on text</tspan> <tspan x="50" dy="15">to get parent node data</tspan>
<tspan x="50" dy="15">and to see the text node values </tspan>
 <tspan x="50" dy="15">of each line</tspan> </text> </g> </svg>

时间: 2024-08-16 06:27:52

使用javaScript和DOM操作svg元素的相关文章

【JavaScript】DOM操作表格

DOM操作表格 1.利用JavaScript动态增加.删除表格内容 页面如图 注:首先说个小问题,有些如制定按钮点击事件的js代码,放在html文档最后可以,放在最前面(<head>标签内)不可以,原因是html文档是自上往下读取,当读到最上面的js.代码时,并没有读取相应的DOM标签,所以制定的按钮功能没有效果,如果非要放在上面,可以指定到window.onload方法中去: <head> <span style="white-space:pre">

【JavaScript】DOM操作下拉框

DOM操作下拉框 这次要实现一个效果,如下图: 在左边选框里选择一个选项,点击"-->",就会把该选项选到右边框里. 在左边选框里选择多个选项,点击"==>",就会把多个选项选到右边框里. 在右边选框里选择一个选项,点击"-->",就会把该选项选到左边框里. 在右边选框里选择多个选项,点击"==>",就会把多个选项选到左边框里. 效果就是这样,下面我们来实现它: 实现源代码: <!DOCTYPE

javascript拓展DOM操作 prependChild insertAfert_DOM

以下是自己写的一个拓展函数,虽然网上已经有了,仅当做自己练兵... 复制代码 代码如下: function prependChild(o,s){ if(s.hasChildNodes()){ s.insertBefore(o,s.firstChild); }else{ s.appendChild(o); } } function insertAfert(o,s){ if(s.nextSibling!=null){ s.parentNode.insertBefore(o,s.nextSibling

JavaScript框架操作DOM改变网页元素的内容和外观

DOM操作 在此之前,你已经看到JavaScript框架使用选择器和DOM遍历可以很容易的获得特定的元素.但是,为了改变网页上特定元素的内容和外观,你需要操作DOM并应用改变.使用纯粹的JavaScript将是一件繁重的工作,但幸运的是,大多数JavaScript框架提供有用的函数,可以很容易地做到这些. 假设你有一个ID为the-box的盒子. <div id="the-box">Message goes here</div> 使用jQuery将其文本改变为&

整理JavaScript对DOM中各种类型的元素的常用操作_基础知识

节点类型nodeType以下是一些重要的nodeType的取值: 1: 元素element 2: 属性attr 3: 文本text 8: 注释comments 9: 文档document nodeName,nodeValue 节点关系childNodes: 每个节点都有一个childNodes属性,其中保存着一个NodeList对象 firstChild: 等同于childNodes[0] lastChild: 等同于childNodes.length-1 同时通过使用列表中每个节点的previ

javascript操作select元素实例分析

 这篇文章主要介绍了javascript操作select元素的方法,可实现针对select元素选中元素时动态改变html元素的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了javascript操作select元素的用法.分享给大家供大家参考.具体分析如下: 这里熟悉一下js对select元素的操作,html页面中建立一个form,其中包含一个select元素和submit按钮. 当选择select中某一项时改变其文字,当select中所有项的文字都改变后,重新恢复它们.

JavaScript DOM操作表格及样式_基础知识

一 操作表格 <table>标签是HTML中结构最为复杂的一个,我们可以通过DOM来创建生成它,或者HTMLDOM来操作它; // 使用DOM来创建表格; var table = document.createElement('table'); table.border = 1; table.width = 300; var caption = document.createElement('caption'); table.appendChild(caption); caption.appe

JavaScript——DOM操作——Window.document对象详解_javascript技巧

一.找到元素:     docunment.getElementById("id"):根据id找,最多找一个:     var a =docunment.getElementById("id");将找到的元素放在变量中:     docunment.getElementsByName("name"):根据name找,找出来的是数组:     docunment.getElementsByTagName("name"):根据标签

javascript操作select元素实例分析_javascript技巧

本文实例讲述了javascript操作select元素的用法.分享给大家供大家参考.具体分析如下: 这里熟悉一下js对select元素的操作,html页面中建立一个form,其中包含一个select元素和submit按钮. 当选择select中某一项时改变其文字,当select中所有项的文字都改变后,重新恢复它们. 当按下submit时关闭窗口本身,代码如下: <!DOCTYPE html> <html> <head> <title>duang for se