[英]How to append a font awesome icon to container
I have a button that creates two input fields when someone clicks on the add button.我有一个按钮,当有人单击添加按钮时,它会创建两个输入字段。 I want to add a font awesome icon right next to the second input field.
我想在第二个输入字段旁边添加一个很棒的字体图标。 I tried adding it by using
appendChild
and innerHTML
but that doesn't work at all I just don't get the icon rendered, how can I do it?我尝试使用
appendChild
和innerHTML
添加它,但这根本不起作用我只是没有渲染图标,我该怎么做? Here is my code:这是我的代码:
const mainParent = document.getElementById('main-parent');
const newPlatformNameInput1 = document.createElement("input");
newPlatformNameInput1.id = index + '_first';
newPlatformNameInput1.classList.add("form-control");
newPlatformNameInput1.classList.add("input");
const newPlatformNameInput2 = document.createElement("input");
newPlatformNameInput2.id = index + '_second';
newPlatformNameInput2.classList.add("form-control");
newPlatformNameInput2.classList.add("input");
const wrapperParent = document.createElement('div');
wrapperParent.id = index + '_parent';
wrapperParent.appendChild(newPlatformNameInput1);
wrapperParent.appendChild(newPlatformNameInput2);
mainParent.appendChild(wrapperParent);
mainParent.appendChild('<i class="fas fa-trash"></i>');
AppendChild expects a node to be passed not plain HTML. AppendChild 期望传递的节点不是普通的 HTML。
icon = document.createElement("i");
icon.setAttribute("class","fas fa-trash");
mainParent.appendChild(icon);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.