javascript - Fontawesome 图标文本出现而不是图标

标签 javascript jquery font-awesome

<分区>

jquery 3.3.1

我正在动态构建 anchor 标记:

 var link = $("<a>");
        link.attr("href", "#");
        link.text("My anchor" + ' <i class="fas fa-people-carry"></i>');

以下生成文字文本 'My anchor i class="fas fa-people-carry"/i' 而不是 anchor 标记的文本和实际图标。

如何使实际图标出现在我的文本旁边的 anchor 标记内?

最佳答案

你需要 jQuery html()

link.html("My anchor" + ' <i class="fas fa-people-carry"></i>');

关于 .text() 的一些说明:

... be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML

关于javascript - Fontawesome 图标文本出现而不是图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51236801/

相关文章:

javascript - 为什么 jQuery 无法触发对 anchor 标记的 native 点击?

jquery - jQuery 快速问题

wordpress - 更改 Font Awesome 图标中的字体大小

javascript - 使用 React 和 react-fa 在输入文本背景上动态放置一个 Font Awesome 图标

javascript - 当页面没有 ScriptManager 时跳过 JavaScript 'Sys.WebForms..' 函数

JavaScript表单验证函数: Looping each item

javascript - 输入元素的值在 Jquery 中无法正常工作

javascript - 如何让AngularJS默认选中一个复选框

this - 将事件传递给外部函数

javascript - 调整 vis js 中带有标签的节点的大小,例如圆形、方框