【问题标题】:Embedding HTML into SVG - How can I render the HTML markup correctly in HTML namespaced elements, inline with my SVG?Embedding HTML into SVG - 如何在 HTML 命名空间元素中正确呈现 HTML 标记,与我的 SVG 内联?
【发布时间】:2020-01-28 23:56:15
【问题描述】:

我正在制作一个交互式 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;
        }
      }
    }

  • 由某些 SVG 元素上的点击事件调用的函数,并使用上述函数从 JSON 中获取点击元素的解释,并将 infodata.title 和 infodata.info 插入到 infoTitle 和 infoText , 在 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>

除了 info 部分中的 HTML 标记之外,所有功能都完美无缺,尽管 xhtml 命名空间和 html 父类型 &lt;h4&gt; 和 &lt;p&gt; 在 infopop 中逐字呈现。 (&lt;h4&gt; & &lt;p&gt; 做 顺便提一下 CSS 样式就好了)

如何在 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" : "&amp;lt;ol&amp;gt;&amp;lt;li&amp;gt;Research who your users are&amp;lt;/li&amp;gt; &amp;lt;li&amp;gt;Plan your research&amp;lt;/li&amp;gt; &amp;lt;li&amp;gt;Interview your users&amp;lt;/li&amp;gt; &amp;lt;li&amp;gt;Write up the requirements&amp;lt;/li&amp;gt; &amp;lt;li&amp;gt;Work with your design team and development team to architect your solution and design the user experience&amp;lt;/li&amp;gt; &amp;lt;/ol&amp;gt;"

【问题讨论】:

    标签: html svg xhtml


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢罗伯特。这适用于 JSON 字符串,例如 "info" : "&amp;lt;i&amp;gt;This is DevOps step six&amp;lt;/i&amp;gt;",但不适用于我上面添加的更丰富的示例。
    猜你喜欢
    • 2014-06-12
    • 2011-06-26
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2012-02-19
    • 2013-01-15
    相关资源
    最近更新 更多