简体   繁体   English

如何将 SVG 添加到我的 Javascript.textContent?

[英]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.

相关问题 如何使用 JavaScript 添加数组中的值作为 textContent - How to add values from an array as textContent with JavaScript 在 Javascript 中添加指向 .textcontent 的链接 - add link to .textcontent in Javascript 如何使用 innerHTML 在 javascript 中添加 svg html 树? - How do I add an svg html tree in javascript using innerHTML? 如何选择随机文本内容? - How do I choose a random textContent? 如何获取分配给带有 textContent 的变量的内容,以作为 javascript 中我的播放器输入的参数? - how can i get what is being assigned to a variable with textContent to act as an argument for my player input in javascript? Javascript:如何在自定义跨度复选框后显示 append label textContent? - Javascript: How do I append the label textContent after a custom span checkbox? 如何在 javascript 中使用.textContent 获取用户输入并在屏幕上显示问候语? - How do I take a users input and display a greeting on the screen using .textContent in javascript? 如何将此jQuery添加到我的Javascript中? - How do I add this jQuery into my Javascript? 如何将Viewdata添加到Javascript? - How do I add Viewdata to my Javascript? 如何在 Javascript 中使用 textContent.substring()? - How do you use textContent.substring() in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM