如何将 javascript 与 svg 一起使用?
how to use javascript with svg?
我已经尝试将 JavaScript 放入 SVG 中,并且都在 html 文件中,
当我按照我的代码中显示的方式编写时,我的 SVG 绘图消失了,那么正确的写法是什么 JavaScript 以便显示 SVG 并且可以对其应用该功能。
<html>
<head>
</head>
<body>
<aside>
height(rx) : <input type="number" name="number" id = "ipheight"
min ="0" max = "50">
<input type="button" value="Resize" onclick="resize()">
<input type="range" id="fthick" min="0" max="10" onchange="thickness()"><br>
<input type="radio" name="copies" id="one">One</br>
<input type="radio" name="copies" id="two">Two</br>
<input type="radio" name="copies" id="Three">Three</br>
<input type="radio" name="copies" id="Four">Four</br>
</aside>
</body>
<script type="text/javascript">
<?xml version="1.0"?>
<svg width="500" height="600"
viewPort="0 0 1200 1200" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<circle cx="315" cy="300" r="15" style="fill:none;stroke:black;stroke-width:1"/>
<g id = "first" style="fill:none;stroke:black;stroke-width:1">
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(45 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(90 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(135 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(180 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(225 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(270 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(315 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(360 315 300)"/>
</g>
<g id= "second" style="stroke:black;stroke-width:1">
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(45 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(45 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(90 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(90 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(135 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(135 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(180 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(180 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(225 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(225 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(270 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(270 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(315 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(315 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(360 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(360 315 300)"/>
</g>
<g id = "third" style="fill:none;stroke:black;stroke-width:1">
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(45 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(90 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(135 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(180 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(225 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(270 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(315 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(360 315 300)"/>
</g>
function resize() {
var resno = ipheight.value;
document.getElementById("third").setAttribute("resno");
</script>
</svg>
</html>
我还没有完全测试实际的脚本代码,但是如果你像我在下面所做的那样在 <svg></svg>
标签之后移动 javascript 块:
</svg>
<script type="text/javascript">
function resize() {
var resno = ipheight.value;
document.getElementById("third").setAttribute("resno");
</script>
</html>
你应该能看到你的图像并测试你的功能。
我已经尝试将 JavaScript 放入 SVG 中,并且都在 html 文件中, 当我按照我的代码中显示的方式编写时,我的 SVG 绘图消失了,那么正确的写法是什么 JavaScript 以便显示 SVG 并且可以对其应用该功能。
<html>
<head>
</head>
<body>
<aside>
height(rx) : <input type="number" name="number" id = "ipheight"
min ="0" max = "50">
<input type="button" value="Resize" onclick="resize()">
<input type="range" id="fthick" min="0" max="10" onchange="thickness()"><br>
<input type="radio" name="copies" id="one">One</br>
<input type="radio" name="copies" id="two">Two</br>
<input type="radio" name="copies" id="Three">Three</br>
<input type="radio" name="copies" id="Four">Four</br>
</aside>
</body>
<script type="text/javascript">
<?xml version="1.0"?>
<svg width="500" height="600"
viewPort="0 0 1200 1200" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<circle cx="315" cy="300" r="15" style="fill:none;stroke:black;stroke-width:1"/>
<g id = "first" style="fill:none;stroke:black;stroke-width:1">
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(45 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(90 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(135 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(180 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(225 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(270 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(315 315 300)"/>
<ellipse cx="355" cy="300" rx="25" ry="7" transform="rotate(360 315 300)"/>
</g>
<g id= "second" style="stroke:black;stroke-width:1">
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(45 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(45 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(90 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(90 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(135 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(135 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(180 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(180 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(225 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(225 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(270 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(270 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(315 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(315 315 300)"/>
<line x1="380" y1="319" x2="420" y2="300" transform="rotate(360 315 300)"/>
<line x1="420" y1="300" x2="380" y2="280" transform="rotate(360 315 300)"/>
</g>
<g id = "third" style="fill:none;stroke:black;stroke-width:1">
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(45 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(90 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(135 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(180 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(225 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(270 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(315 315 300)"/>
<ellipse cx="390" cy="300" rx="60" ry="20" transform="rotate(360 315 300)"/>
</g>
function resize() {
var resno = ipheight.value;
document.getElementById("third").setAttribute("resno");
</script>
</svg>
</html>
我还没有完全测试实际的脚本代码,但是如果你像我在下面所做的那样在 <svg></svg>
标签之后移动 javascript 块:
</svg>
<script type="text/javascript">
function resize() {
var resno = ipheight.value;
document.getElementById("third").setAttribute("resno");
</script>
</html>
你应该能看到你的图像并测试你的功能。