简体   繁体   English

如何使用javascript和html动态地向按钮添加字体真棒图标?

[英]how to add font awesome icon to a button dynamically using javascript and html?

i want to create the html code below dynamically using javascript我想使用javascript动态创建下面的html代码

<button id="add_row" style="background-color:#90EE90;"  onclick="add(event)" >
                             <i class="fa fa-plus"  style="font-size: 2em;"></i> 
                          </button>

this is how create every element separately这就是分别创建每个元素的方式

          var deletebutton =  document.createElement("INPUT");
        deletebutton.setAttribute("type", "button");
      deletebutton.setAttribute("onclick", "add('div" + i + "')");

      var icondelete =  document.createElement("i");
      icondelete.setAttribute("class", "fa fa-times");
          icondelete.setAttribute("style", "font-size: 2em;");

so how to put the icon icondelete inside the button deletebutton那么如何将图标icondelete放在按钮deletebutton

Since icondelete is a node and so is deletebutton, you can use appendChild由于 icondelete 是一个节点,deletebutton 也是一个节点,因此您可以使用 appendChild

deletebutton.appendChild(icondelete);

for the click handler you can do this对于点击处理程序,您可以执行此操作

deletebutton.addEventListener("click",function(evt){
   add(evt)
});

Also you need to use BUTTON not input for the button, inputs can't have HTML children or any children for that matter.此外,您需要使用 BUTTON not input 按钮,输入不能有 HTML 子项或任何子项。

var deletebutton =  document.createElement("button");

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

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