React contenteditable svg text isn't editable

ⅰ亾dé卋堺 提交于 2019-12-24 07:14:24

问题


https://codepen.io/anon/pen/YaGdLV

<svg><text contentEditable="true">HELLO</text></svg>

That's an old version of React, because I quickly grabbed a codepen, but I'm using the latest react on my own project and it's the same issue.

I get the warning about contenteditable, but even still I simply can't edit text elements in svg.


回答1:


By default svg does not support contenteditable attribute.

<div style="border:1px solid" >
  <div>
    Here svg has contenteditable attribute, but this does not work
    <svg contenteditable height="30" width="200" style="border: 1px solid">
      <text x="0" y="15">This is SVG</text>
    </svg>
  </div>
</div>

But you can wrap your svg element with aby another block such as div. And your svg element will be changed correctly:

<div style="border:1px solid" >
  <p>Hello</p>
  <div contenteditable>
    <svg height="30" width="200" style="border: 1px solid">
      <text x="0" y="15">This is SVG</text>
    </svg>
  </div>
  <p>World</p>
</div>

Live demo https://codepen.io/xnimorz/pen/NYRoRV



来源:https://stackoverflow.com/questions/49340612/react-contenteditable-svg-text-isnt-editable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!