新建html5文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SVG DOM绘制矩形</title>
<style>
#myline{
border: 1px solid red;
width: 200px;
height: 200px;
}
</style>
<script>
function createLine(){
var root=document.getElementByIdx_x("myline");
var SVG="http://www.w3.org/2000/svg";
//创建我们的SVG元素
var svg=document_createElement_xNS(SVG,"svg");
svg.setAttribute("width","200");
svg.setAttribute("height","200");
//创建我们的line元素
var line=document_createElement_xNS(SVG,"line");
line.setAttribute("x1","20");
line.setAttribute("y1","20");
line.setAttribute("x2","160");
line.setAttribute("y2","160");
line.setAttribute("stroke","blue");
svg.a(line);
root.a(svg);
}
</script>
</head>
<body onload="createLine()">
<h4>SVG DOM绘制线段</h4>
<div id="myline"></div>
</body>
</html>