将 HTML 嵌入 SVG - 如何在 HTML 命名空间元素中正确呈现 HTML 标记,与我的 SVG 内联?

格雷格

我正在制作一个交互式 SVG 图,它最终将嵌入到 HTML 页面中,但我想让该图完全独立。

我在 Illustrator 中创建了基本的 SVG,并添加了一个脚本部分,其中包含以下内容:

  • 分配了一些 JSON 的变量(JSON 看起来像这样)

    var infodata =
        [{
          "id" : "step1",
          "title" : "Step 1",
          "info" : "<i>This is DevOps step one</i>"
        },{etc}];

  • 从单击的 svg 元素获取引用并返回 JSON 中具有该引用的行的函数。

    function findInfo(idValue) {
      for (var i=0; i < infodata.length; i++){
        if (infodata[i]['id'] == idValue){
          return (infodata[i]);
          break;
        }
      }
    }

  • 由一个click事件调用某些SVG元素,并使用上述获得从JSON和嵌入式点击的ELEM的解释功能的功能infodata.titleinfodata.infoinfoTitleinfoText,在infopop区域。

    function showInfopop(evt){
       thisInfo=evt.target;
       thisInfoRef = thisInfo.getAttributeNS(null,'inforef');
       info = findInfo(thisInfoRef);
       infoTitle = info.title;
       infoText = info.info;
       document.getElementById('infoTitleContent').textContent = infoTitle;
       document.getElementById('infoTextContent').textContent = infoText;
    }

  • SVG 开始标记

    <svg
        id="Layer_1"
        data-name="Layer 1"
        xmlns="http://www.w3.org/2000/svg"
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        width="1369"
        height="828"
        viewBox="0 0 1369 828">

  • 然后是 infopop 组元素的 SVG

    <g id='infopopGroup'>
      <foreignObject
        x="100"
        y="10"
        overflow="scroll"
        width="150px"
        height="100px"
        requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"
        id="foreignObject_test">
        <xhtml:div  xmlns="http://www.w3.org/1999/xhtml"
          class="InfopopRect">
          <xhtml:h4
            class="InfoTitle"
            id="infoTitleContent">
              Info
          </xhtml:h4>
          <xhtml:p
            class="InfoText"
            contentEditable="true"
            id="infoTextContent">
              &lt;i&gt;Click on circles for details of the DevOps lifecycle&lt;/i&gt;
          </xhtml:p>
        </xhtml:div>
      </foreignObject>
    </g>

除了命名空间和 html 父类型&之外,所有功能都完美无缺,除了infoJSON 部分中的 HTML 标记在 infopop 中按字面呈现顺便说一句&确实选择了 css 样式)xhtml<h4><p><h4><p>

如何在 HTML 命名空间元素中正确呈现 HTML 标记,与我的 SVG 内联?

罗伯特的回答适用于基本文本,但不适用于 JSON 中更丰富的标记,例如


    var infodata = 
    [{ "id" : "step1", 
    "title" : "Requirements capture", 
    "info" : "<ol>
    <li>Research which users</li>
    <li>Plan your research</li>
    <li>Interview your users</li>
    <li>Write up the requirements</li>
    <li>Work with design team and development team to architect your solution and design the user experience</li>
    </ol>" },
    { "id" : "step6", 
    "title" : "Step 6", 
    "info" : "&lt;i&gt;This is DevOps step six&lt;/i&gt;" }];

要么


      "id" : "step1",
      "title" : "Requirements capture",
      "info" : "&lt;ol&gt;
      &lt;li&gt;Research who your users are&lt;/li&gt;
      &lt;li&gt;Plan your research&lt;/li&gt;
      &lt;li&gt;Interview your users&lt;/li&gt;
      &lt;li&gt;Write up the requirements&lt;/li&gt;
      &lt;li&gt;Work with your design team and development team to architect your solution and design the user experience&lt;/li&gt;
    &lt;/ol&gt;"

最后 ;-) 这有效!所有一行,没有换行符:

"info" : "&lt;ol&gt;&lt;li&gt;Research who your users are&lt;/li&gt; &lt;li&gt;Plan your research&lt;/li&gt; &lt;li&gt;Interview your users&lt;/li&gt; &lt;li&gt;Write up the requirements&lt;/li&gt; &lt;li&gt;Work with your design team and development team to architect your solution and design the user experience&lt;/li&gt; &lt;/ol&gt;"

罗伯特·朗森

您需要使用 innerHTML 而不是 textContent 来设置 infoText 例如

document.getElementById('infoTextContent').innerHTML = infoText;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将HTML元素放置在内联SVG上

来自分类Dev

如何在SVG中使用内联HTML元素?

来自分类Dev

如何使SVG嵌入式HTML内联加载其xlink:href依赖项?

来自分类Dev

如何在SVG标签之间嵌入html代码

来自分类Dev

我如何将元素内联html

来自分类Dev

如何获取嵌入HTML的SVG的onclick事件以显示在HTML中?

来自分类Dev

如何在HTML5的内联SVG中读取自定义名称空间节点?

来自分类Dev

将SVG图嵌入SAS的HTML输出文件中

来自分类Dev

内联html的SVG无法正确显示(按Chrome)

来自分类Dev

HTML SVG元素无法正确呈现:

来自分类Dev

如何将svg路径移出html

来自分类Dev

将.SVG文件的内容插入HTML元素

来自分类Dev

HTML SVG如何在鼠标悬停时将元素更改为可见/隐藏?

来自分类Dev

如何判断DOM元素是HTML还是SVG?

来自分类Dev

如何在HTML中使用外部SVG?

来自分类Dev

如何在HTML中使用SVG?

来自分类Dev

如何在HTML中翻转SVG路径?

来自分类Dev

HTML5内联SVG自动裁剪空白

来自分类Dev

如何正确地将几个svg元素附加到html文档?

来自分类Dev

HTML 中的内联 SVG - 相同的字体大小呈现得更大

来自分类Dev

如何将ruby / erb代码嵌入到内联html样式标签中?(进度条)

来自分类Dev

SVG 在嵌入 HTML 时不显示。单独显示就好

来自分类Dev

将HTML标记嵌入rails标记内

来自分类Dev

如何使用html canvas将SVG图形导出为pdf

来自分类Dev

如何将svg插入跨多行的html表

来自分类Dev

将SVG与其他HTML元素垂直对齐

来自分类Dev

将包含SVG元素的HTML转换为图像文件

来自分类Dev

尝试将SVG与HTML元素对齐时的间隙

来自分类Dev

将HTML SVG文本旋转270度