javascript - appendChild is not working, firefox on mac -
var xmlns = "http://www.w3.org/2000/svg"; var xlinkns = "http://www.w3.org/1999/xlink"; var arrivedv; var arrivednv; var count = 0; var timerfunction = null; var svgelement = document.getelementbyid("van"); svgelement.addeventlistener("mousedown", mousedown); function mousedown(e) { var x = e.clientx; var y = e.clienty; var coords = "x : " + x + ", y : " + y; document.getelementbyid("mpost").value = coords; addtruck(x, y); } function addtruck(x, y) { var rect = document.createelementns("http://www.w3.org/2000/svg", "rectangle"); rect.setattributens(null, "x", x); rect.setattributens(null, "y", y); rect.setattributens(null, "width", 20); rect.setattributens(null, "height", 20); rect.setattributens(null, "fill", "#800000"); rect.setattributens(null, "stroke", "none"); document.getelementbyid("svpg").appendchild(rect); }
<svg id="svpg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="934" height="502" style="border:1px solid #d3d3d3;" onmousedown="mousedown(e);"> <image id="van" xlink:href="/uploads/2/1/7/6/21767914/4792239_orig.png" x="0" y="0" height="100%" width="100%" /> <rect id="seabus1" x="445" y="320" width="5" height="10" style="fill:rgb(0,0,255);" transform="rotate(36 445 320)" /> <rect id="seabus2" x="590" y="130" width="5" height="10" style="fill:rgb(0,0,255);" transform="rotate(36 590 130)" /> <rect id="tank1" x="50" y="180" width="20" height="5" style="fill:rgb(0,0,0);" transform="rotate(10 50 180)" /> <rect id="tank2" x="40" y="280" width="20" height="5" style="fill:rgb(255,0,0); transform = " rotate(7 50 180) "" /> <rect id="cargo1" x="498" y="296" width="20" height="5" style="fill:rgb(165,42,42);" transform="rotate(27 498 296)" /> </svg> <input type="text" id="mpost" value="">
i trying add rectangle position of mouse.
i have tried lot of other solution online still no luck. have tried append img no success. based on see online should work don't see missing. using firefox.
you're getting error onmousedown="mousedown(e)"
, because there's no variable e
. name of global variable contains current event when use inline handler event
, should onmousedown="mousedown(e)"
.
alternatively, can use
document.getelementbyid("svpg").addeventlistener("mousedown", mousedown);
i'm not sure if need have mousedown
handlers on both <svg>
, <image>
.
var xmlns = "http://www.w3.org/2000/svg"; var xlinkns = "http://www.w3.org/1999/xlink"; var arrivedv; var arrivednv; var count = 0; var timerfunction = null; var svgelement = document.getelementbyid("van"); svgelement.addeventlistener("mousedown", mousedown); function mousedown(e) { var x = e.clientx; var y = e.clienty; var coords = "x : " + x + ", y : " + y; document.getelementbyid("mpost").value = coords; addtruck(x, y); } function addtruck(x, y) { var rect = document.createelementns("http://www.w3.org/2000/svg", "rectangle"); rect.setattributens(null, "x", x); rect.setattributens(null, "y", y); rect.setattributens(null, "width", 20); rect.setattributens(null, "height", 20); rect.setattributens(null, "fill", "#800000"); rect.setattributens(null, "stroke", "none"); document.getelementbyid("svpg").appendchild(rect); }
<svg id="svpg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="934" height="502" style="border:1px solid #d3d3d3;" onmousedown="mousedown(event);"> <image id="van" xlink:href="/uploads/2/1/7/6/21767914/4792239_orig.png" x="0" y="0" height="100%" width="100%" /> <rect id="seabus1" x="445" y="320" width="5" height="10" style="fill:rgb(0,0,255);" transform="rotate(36 445 320)" /> <rect id="seabus2" x="590" y="130" width="5" height="10" style="fill:rgb(0,0,255);" transform="rotate(36 590 130)" /> <rect id="tank1" x="50" y="180" width="20" height="5" style="fill:rgb(0,0,0);" transform="rotate(10 50 180)" /> <rect id="tank2" x="40" y="280" width="20" height="5" style="fill:rgb(255,0,0); transform = " rotate(7 50 180) "" /> <rect id="cargo1" x="498" y="296" width="20" height="5" style="fill:rgb(165,42,42);" transform="rotate(27 498 296)" /> </svg> <input type="text" id="mpost" value="">
Comments
Post a Comment