add dynamically foreign object in svg using javascript -


when run code shows me blank screen when update code using developer tool in chrome shows data. please explanation why shows when update code using developer tool of chrome, due dom @ browser runs again, if yes why not @ 1 first time shows. happen due foreignobject.

<!doctype html>  <html lang="en">     <head>         <meta charset="utf-8" />         <title></title>     </head>     <body>     <svg id="t">     <g>         <text x="10" y="10">hello</text>     </g>     </svg>         <script>             var s = document.getelementbyid('t');             var g = s.childnodes[1];             console.log(g.childnodes[1].remove());             var foreign = document.createelementns('http://www.w3.org/2000/svg',"foreignobject");              foreign.setattribute('width', 500);             foreign.setattribute('height', 150);             var txt = document.createelementns('http://www.w3.org/2000/svg', 'text');             txt.setattribute('x', '10');             txt.setattribute('y', '10');             var t = document.createtextnode("this paragraph.");             txt.appendchild(t);             foreign.appendchild(txt);             g.appendchild(foreign);   </script>         </body> </html> 

an svg text node cannot child of foreignobject node, need svg node in way. e.g.

        var s = document.getelementbyid('t');          var g = s.childnodes[1];          console.log(g.childnodes[1].remove());          var foreign = document.createelementns('http://www.w3.org/2000/svg',"foreignobject");            foreign.setattribute('width', 500);          foreign.setattribute('height', 150);          var svg = document.createelementns('http://www.w3.org/2000/svg', 'svg');          var txt = document.createelementns('http://www.w3.org/2000/svg', 'text');          txt.setattribute('x', '10');          txt.setattribute('y', '30');          var t = document.createtextnode("this paragraph.");          txt.appendchild(t);          foreign.appendchild(svg);          svg.appendchild(txt);          g.appendchild(foreign);
<html lang="en">      <head>          <meta charset="utf-8" />          <title></title>      </head>      <body>      <svg id="t">      <g>          <text x="10" y="30">hello</text>      </g>      </svg>  </body>  </html>

having said that, don't see why you'd want use foreignobject unless you're going create non-svg nodes.

the other thing trips people creating elements in correct namespace. svg elements need go in svg namespace(http://www.w3.org/2000/svg).

perhaps rerunning forces chrome create svg parent node or perhaps it's chrome bug.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -