SVG foreignObject 未在任何浏览器上显示,为什么?

Edy*_*rne 6 html xhtml svg

我在 SVG 元素中有一个 foreignObject 。所有其他元素都显示,但foreignObject 与其内容一起不可见。在 Chrome、Firefox 和 Edge 中进行测试,结果都相同。

这是代码:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="v-2" width="100%" height="100%">
    <g id="v-3" class="joint-viewport" transform="matrix(1,0,0,1,-1597.0002028000001,95.99995439999998)">
        ...
        <g id="j_29" model-id="e8dbd7a4-5d3d-44e5-85a0-09413112a39b" class="joint-theme-default joint-cell joint-type-html joint-type-html-element joint-element" data-type="html.Element" fill="#ffffff" stroke="none" transform="translate(1898.0001898,268.0000346)">
            <g class="rotatable" id="v-206">
                <rect class="body" id="v-207" stroke="none" fill-opacity="0" fill="#ffffff" width="100" height="60"></rect>
                <text class="label" id="v-208" font-size="14" y="0.8em" display="none" xml:space="preserve" fill="#000000" text-anchor="middle" font-family="Arial, helvetica, sans-serif" transform="matrix(1,0,0,1,125,20)">
                    <tspan id="v-209" class="v-line v-empty-line" dy="0em" x="0" style="fill-opacity: 0; stroke-opacity: 0;">-</tspan>
                </text>
                <foreignObject requiredExtensions="http://www.w3.org/1999/xhtml" width="100%" height="100">
                    <body xmlns="http://www.w3.org/1999/xhtml">
                        <input xmlns="http://www.w3.org/1999/xhtml" type="text" value="I'm HTML input">
                    </body>
                </foreignObject>
            </g>
        </g>
    </g>
    <defs id="v-4"></defs>
</svg>
Run Code Online (Sandbox Code Playgroud)

问:我做错了什么?

更新:我注意到一些事情:

  • 添加xmlns="http://www.w3.org/1999/xhtml"到输入使其显示在 Edge 上。在其他浏览器上,它仍然不可见。
  • 在 Chrome 上,如果我<g>通过选择“编辑为 HTML”来编辑外部元素,但随后不进行任何更改并退出编辑模式,则会显示输入。
  • 添加requiredExtensions="http://www.w3.org/1999/xhtml"foreignObject标签和/或正文标签没有区别。

我肯定错过了什么...

Vla*_*kov 18

2019年答案:

  • foreignObject应该有xywidthheight定义
  • 添加xmlns:xhtml="http://www.w3.org/1999/xhtml"到您的<svg ...标签定义
  • SVG 中的所有 HTML 标签都应以xhtml:前缀开头

例子:

<svg class="front" viewBox="0 0 376 244" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xhtml="http://www.w3.org/1999/xhtml">
...
    <foreignObject x="0" y="165" width="100%" height="38">
      <xhtml:span class="copy-popover">Click to Copy</xhtml:span>
    </foreignObject>
...
</svg>
Run Code Online (Sandbox Code Playgroud)

  • 英雄!经过一番挖掘后救了我。 (3认同)