[英]How to add icon in a cell before text
I want to add to the table cell a Font-Awesome
icon. 我想在表格单元格中添加一个Font-Awesome
图标。 But I need to insert it before this cell text. 但是我需要在此单元格文本之前插入它。 How to do it via JavaScript
or Jquery
? 如何通过JavaScript
或Jquery
做到这一点?
That's my code: 那是我的代码:
var song_nameL = tr.insertCell(0);
song_nameL.innerHTML = songName;
var fa_times = song_nameL.appendChild(document.createElement("i"));
fa_times.className = "fa fa-times";
As you see, it appears after the text. 如您所见,它出现在文本之后。 And if I put song_nameL.innerHTML = songName;
如果我放song_nameL.innerHTML = songName;
to the end of this code, there will be no icon. 到此代码末尾,将没有任何图标。 I tried to increase zIndex
of icon, but it didn't help. 我试图增加图标的zIndex
,但没有帮助。
You have to prepend
that <i>
element, 你必须prepend
那个<i>
元素,
var fa_times = song_nameL.insertAdjacentHTML('afterbegin',"<i class='fa fa-times'></i>"));
by using .insertAdjacentHTML("position", "htmlString");
通过使用.insertAdjacentHTML("position", "htmlString");
<i>
元素的HTML代码应在歌曲名称之前/之前,因此请执行以下操作:
song_nameL.innerHTML = '<i class="fa fa-times"></i>' + song_nameL;
if you need the object you could use: 如果您需要该对象,则可以使用:
var fa_times = document.createElement("i");
fa_times.className = "fa fa-times";
song_nameL.insertBefore(fa_times, song_nameL.childNodes[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.