[英]How Do I Add an SVG to My Javascript .textContent?
I am simply trying to insert an exclamation SVG here, but I can't seem to do it, and I am unable to find an adequate answer on Google.我只是想在这里插入感叹号 SVG,但我似乎做不到,而且我无法在 Google 上找到合适的答案。
The SVG is downloaded, and contained within my project folder. SVG 已下载,并包含在我的项目文件夹中。
if(email.validity.valueMissing) {
emailError.textContent = '(SVG Here) You need to enter an e-mail address.';
Assuming e-mailError
is a display element in your html (span, p, div etc.) the icon and associated text can be loaded by setting the .innerHTML
property of the element.假设
e-mailError
是 html 中的显示元素(span、p、div 等),可以通过设置元素的.innerHTML
属性来加载图标和相关文本。
The use of .textContent
will result in the markup text being displayed rather than the intended layout (as you have found)使用
.textContent
将导致显示标记文本而不是预期的布局(如您所见)
This working snippet demonstrates the difference.这个工作片段展示了差异。
const emailError=document.getElementsByClassName("emailError"); emailError[0].innerHTML = '<img src="https://stackoverflow.design/assets/img/logos/sf/sf-icon.svg" /> You need to enter an e-mail address.'; emailError[1].textContent = '<img src="https://stackoverflow.design/assets/img/logos/sf/sf-icon.svg" /> You need to enter an e-mail address.';
img { width: 40px; aspect-ratio: 1; } span { border: 1px solid black; display: flex; align-items: center; }
<p>Example loaded using.innerHTML:</p> <p><span class="emailError"></span></p> <p>Example loaded using.textContent:</p> <p><span class="emailError"></span></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.