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

问题描述 投票:0回答:2

我在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>

问:我做错了什么?

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

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

我肯定错过了什么...

html5 svg xhtml
2个回答
3
投票

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>

2
投票

诀窍是从requiredExtensions="http://www.w3.org/1999/xhtml标签中删除foreignObject。当此属性存在时,Chrome不会显示任何内容,但删除后,Chrome适用于Chrome,IE和Firefox。

不幸的是我在Chrome上遇到了另一个错误(当文本溢出时它将输入内容呈现在一个奇怪的地方),所以我不再使用foreignObjects了,但是为了以防任何人都是古玩,这就是我的工作原理问题。

© www.soinside.com 2019 - 2024. All rights reserved.