Javascript in SVG -


i created svg file contains 5 polygons, need embed javascript 4 of polygons' color changes red when mouseover, , when mouseout, color changes green. tried write code didn't work, problem? , tips!

<!doctype svg public "-//w3c//dtd svg 1.1//en"    "http://www.w3.org/graphics/svg/1.1/dtd/svg11.dtd"> <svg width="26cm" height="24cm" viewbox="0 0 2600 2400" version="1.1"    xmlns="http://www.w3.org/2000/svg"    xmlns:xlink= "http://www.w3.org/1999/xlink"> <script type="text/javascript"> <![cdata[  document.getelementbyid("test").onmouseover = function(){changecolor()};  function changecolor() { document.getelementbyid("test").style.color = "red"; }  document.getelementbyid("test").onmouseout = function(){changecolor()};  function changecolor() { document.getelementbyid("test").style.color = "green"; }  ]]> </script> <circle cx="1600" cy="700" r="600" fill="yellow" stroke="black" stroke-width="3"/> <ellipse id="test" cx="1300" cy="500" rx="74" ry="120" fill="blue" stroke="black"  stroke-width="3" onmouseover="javascript:red();" onmouseout="javascript:green();"/> <ellipse id="test" cx="1850" cy="500" rx="74" ry="120"  fill="blue" stroke="black" stroke-width="3" onmouseover="javascript:red();" onmouseout="javascript:green();"/> <rect id="test" x="1510" y="650" width="160" height="160" fill="blue" stroke="black" stroke-width="3" onmouseover="javascript:red();" onmouseout="javascript:green();"/> <polygon id="test" points="1320,800 1370,1080 1820,1080 1870,800 1820,1000 1370,1000" name="mouth" fill="blue" stroke="black" stroke-width="3" onmouseover="javascript:red();" onmouseout="javascript:green();"/>   </svg> 

you have various errors

  1. you've 2 functions called changecolor, functions must have unique names

  2. svg not use color colour elements, instead uses fill (and stroke).

  3. id values must unique, want replace id class , use getelementsbyclassname instead of getelementbyid. if you'll need cope more 1 element though. i've not completed part, should try understand what's going on.

i've removed 1 id version can see working on left eye.

document.getelementbyid("test").onmouseover = function(){changecolor()};    function changecolor() {  document.getelementbyid("test").style.fill = "red";  }    document.getelementbyid("test").onmouseout = function(){changecolor2()};    function changecolor2() {  document.getelementbyid("test").style.fill = "green";  }
<svg width="26cm" height="24cm" viewbox="0 0 2600 2400" version="1.1"     xmlns="http://www.w3.org/2000/svg"     xmlns:xlink= "http://www.w3.org/1999/xlink">    <circle cx="1600" cy="700" r="600" fill="yellow" stroke="black" stroke-width="3"/>  <ellipse id="test" cx="1300" cy="500" rx="74" ry="120" fill="blue" stroke="black"  stroke-width="3"/>  <ellipse cx="1850" cy="500" rx="74" ry="120"  fill="blue" stroke="black" stroke-width="3" />  <rect x="1510" y="650" width="160" height="160" fill="blue" stroke="black" stroke-width="3" />  <polygon points="1320,800 1370,1080 1820,1080 1870,800 1820,1000 1370,1000" name="mouth" fill="blue" stroke="black" stroke-width="3"/>     </svg>


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -