简体   繁体   English

如何使用 .innerHTML 添加 html 标签?

[英]How can I add html tags using .innerHTML?

I have an element that I want to update the innerhtml text and icon but my tag is being ignored.我有一个要更新innerhtml 文本和图标的元素,但我的标签被忽略了。

 document.getElementById("nextBtn").innerHTML = "Next
 <i class="icon icon--right icon-arrow-right"></i>";
}

You need to escape the inner double quotes " in your string, or change your outer quotes to single quotes ' ; Examples:您需要转义字符串中的内部双引号" ,或者将外部引号更改为单引号' ; 示例:

document.getElementById("nextBtn").innerHTML = "Next <i class=\"icon icon--right icon-arrow-right\"></i>";

document.getElementById("nextBtn").innerHTML = 'Next <i class="icon icon--right icon-arrow-right"></i>';

Your HTML string should be formatted with single quotes instead of double quote .您的 HTML 字符串应使用单引号而不是双引号进行格式化

Current is:目前是:

 document.getElementById("nextBtn").innerHTML = "Next
 <i class="icon icon--right icon-arrow-right"></i>";

Change to:改成:

document.getElementById("nextBtn").innerHTML = 'Next<i class = "icon icon--right icon-arrow-right" ></i>';

你必须用单引号代替双引号

document.getElementById("nextBtn").innerHTML = 'Next<i class = "icon icon--right icon-arrow-right" ></i>';

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

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