简体   繁体   English

获取 [object SVGSVGElement] 而不是 SVG

[英]Getting [object SVGSVGElement] instead of SVG

I have a "group" of SVGs at the bottom of my HTML page, hidden.我的 HTML 页面底部有一组 SVG,隐藏。 I am trying to conditionally use them based on object keys.我正在尝试根据 object 键有条件地使用它们。 For instance, I have:例如,我有:

<svg id="fiftyOne" width="100%" height="100%" viewBox="0 0 2863 871" ...../></svg>

And I want to conditionally call #fiftyOne as the svg logo inside of this Javascript Template:我想有条件地将#fiftyOne称为此 Javascript 模板内的 svg 徽标:

const refreshView = (dataSet) => {
    output.innerHTML = "";
    dataSet.forEach((p) => {
      output.innerHTML += `
        
        <div class="card pickup-result" data-output="${p.output}" data-tone="${p.tone}">
        <div class="card-body">
          <div class="card-heading">
            <h4>${p.name}</h4>${p.svg}   <// Call the SVG here.
          </div>
          <hr>
         
          <h5>Specs:</h5>
          <p><strong>Output: </strong> ${p.output}</p>
          <p><strong>Description:</strong> ${p.description}</p>
  
          <div class="pickup-info">
          <a class="link-button" target="blank" href="${p.url}">See Product</a>
           
          </div>
        </div>
        
        </div>
        `;
    });

But I'm getting the rendered card, with [object SVGSVGElement] instead of the rendered SVG.但是我得到了渲染的卡片,使用[object SVGSVGElement]而不是渲染的 SVG。 Am I doing something wrong?难道我做错了什么?


UPDATE:更新:

Using p.svg.outerHTML works to fill the HTML with the proper information.使用p.svg.outerHTML可以用正确的信息填充 HTML。 The next problem I'm facing is the SVG won't show up on the actual card, even though the data is there.我面临的下一个问题是 SVG 不会出现在实际卡上,即使数据在那里。 See screenshots:见截图: 开发工具截图

Any Ideas?有任何想法吗? This is the first time working with SVGs.这是第一次使用 SVG。

You are putting a live SVG element into the code instead of its source.您将实时 SVG 元素放入代码而不是其源代码中。

You can output p.svg.outerHTML instead of just p.svg .您可以 output p.svg.outerHTML而不仅仅是p.svg


Or if you wanted to include that actual element and not a copy of it, you'd have to insert the element after you rendered the HTML entirely (not after each iteration because element references get invalidated every time you set innerHTML as all the contents are reconstructed):或者,如果您想包含该实际元素而不是它的副本,则必须在完全渲染 HTML 之后插入该元素(不是在每次迭代之后,因为每次设置 innerHTML 时元素引用都会失效,因为所有内容都是重建):

const headers = Array.from(output.querySelectorAll('.card-heading'))
dataSet.forEach((p, index) => headers[index].appendChild(p.svg))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM