![](/img/trans.png)
[英]How to add values from an array as textContent with JavaScript
[英]How Do I Add an SVG to My Javascript .textContent?
我只是想在這里插入感嘆號 SVG,但我似乎做不到,而且我無法在 Google 上找到合適的答案。
SVG 已下載,並包含在我的項目文件夾中。
if(email.validity.valueMissing) {
emailError.textContent = '(SVG Here) You need to enter an e-mail address.';
假設e-mailError
是 html 中的顯示元素(span、p、div 等),可以通過設置元素的.innerHTML
屬性來加載圖標和相關文本。
使用.textContent
將導致顯示標記文本而不是預期的布局(如您所見)
這個工作片段展示了差異。
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.