简体   繁体   English

字体很棒的图标和面板在使用 JS 时无法正确显示

[英]Font awesome icons and panels not showing properly when using JS

I'm trying to create HTML elements dynamically with JS.我正在尝试使用 JS 动态创建 HTML 元素。 However panels and font awesome icons are not shown properly when doing so.但是,这样做时面板和字体真棒图标无法正确显示。

This is the HTML code I want to create dynamically with JS.这是我想用 JS 动态创建的 HTML 代码。

<div class="row" id="row">
    <div class="col">
        <div class="panel-custom border colorized">
            <div class="fa fa-chevron-right right-arrow"></div>
        </div>
    </div>
</div>

My JS code:我的JS代码:

  let row = document.getElementById('row');
  let col = document.createElement('col');
  let panelCustom = document.createElement('panel-custom');
  let fa = document.createElement('fa');

  panelCustom.classList.add("colorized");
  panelCustom.classList.add("border");
  fa.classList.add("fa-chevron-right");
  fa.classList.add("right-arrow");

  row.appendChild(col);
  col.appendChild(panelCustom);
  panelCustom.appendChild(fa);

You can try this ( it should work as expected ) :你可以试试这个(它应该按预期工作):

 let row = document.createElement('div'); row.className = "row"; let col = document.createElement('div'); col.className = "col"; let panelCustom = document.createElement('div'); panelCustom.className = "panel-custom colorized border"; let fa = document.createElement('i'); fa.className = "fa fa-chevron-right right-arrow"; panelCustom.appendChild(fa); col.appendChild(panelCustom); row.appendChild(col); document.body.appendChild(row);
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet"/>

You can't actually create elements of type "col", "panel-custom" or "fa", like you're trying to here:您实际上无法创建“col”、“panel-custom”或“fa”类型的元素,就像您在这里尝试的那样:

let col = document.createElement('col');
let panelCustom = document.createElement('panel-custom');
let fa = document.createElement('fa');

These are not valid HTML elements.这些不是有效的 HTML 元素。 In fact, they should be <div> elements as in your HTML sample.实际上,它们应该是 HTML 示例中的<div>元素。 Create <div> elements instead:改为创建<div>元素:

let col = document.createElement('div');
let panelCustom = document.createElement('div');
let fa = document.createElement('div');

Then add the required classes:然后添加所需的类:

panelCustom.classList.add("panel-custom");
panelCustom.classList.add("colorized");
panelCustom.classList.add("border");
// etc.

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

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