[英]How to get an object as a string in innerHTML?
So I'm targeting my svg and its surrounding div: 所以我的目标是我的svg及其周围的div:
const svg = e.target.parentElement;
Then I have ${svg}
in the following context: 然后在以下情况下有${svg}
:
result.innerHTML = <h1 class="text-win">You won</h1> <div class="result"> ${svg} </div>
But I always get ${svg}
as [object HTMLDivElement]
. 但是我总是得到${svg}
作为[object HTMLDivElement]
。 How would I get the svg
to display as a string instead of an object? 我将如何将svg
显示为字符串而不是对象?
parentElement
is an HTML DOM Element Object . parentElement
是一个HTML DOM元素对象 。
When you inject it using the ${svg}
, you actually call its toString()
method, which returns the string [object HTMLDivElement]
. 当使用${svg}
注入它时,实际上调用了它的toString()
方法,该方法返回字符串[object HTMLDivElement]
。
As suggested, if you'll use the outerHTML
property of svg
, you'll get the result you're expecting: 如建议的那样,如果您使用svg
的outerHTML
属性,您将得到期望的结果:
Reading the value of
outerHTML
returns aDOMString
containing an HTML serialization of the element and its descendants 读取outerHTML
的值将返回一个DOMString
其中包含元素及其后代的HTML序列化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.