在字符串前附加 html
Prepend additoonal html to string
我只接受原始 js 答案,jquery 会被否决。
我有以下字符串,我们称之为 MyHtmlAbove
:
<span class="fc-title-wrap"><span class="fc-title fc-sticky">Some Sample Content here</span></span>
我知道我能做到:
el = document.createElement('div');
el.innerHTML = myHtmlAbove;
我现在不知道怎么办的是:
找到 class fc-title
和内容(前置)和图标标记之前:<i class='font-awesome-here'></i>
我不确定我是否可以只使用普通的 raw:
el.findByClassName('fc-title').prepend('<i class='font-awesome-here'></i> ')
// Result should be:
<span class="fc-title-wrap"><span class="fc-title fc-sticky"><i class='font-awesome-here'></i> Some Sample Content here</span></span>
谁能给我指出正确的方向,我觉得我很接近。
因为我已经创建了一个临时元素来执行此操作,所以我还想清理它,这样它就不会出现在 dom 一些不应该出现的地方。
想法?
document.getElementsByClassName('fc-title')[0].prepend('<i class="font-awesome-here"></i> ')
// Result should be:
// <span class="fc-title-wrap"><span class="fc-title fc-sticky"><i class='font-awesome-her
<span class="fc-title-wrap"><span class="fc-title fc-sticky">Some Sample Content here</span></span>
document.getElementsByClassName('fc-title')[0].prepend('<i class="font-awesome-here"></i> ')
原始 Js() 版本:
const fcT = document.querySelector(".fc-title");
const t = fcT.innerText
const icon = "<i class='font-awesome-here'></i>"
fcT.innerHTML = icon + " " + t;
console.log(document.querySelector(".fc-title"));
<span class="fc-title-wrap"><span class="fc-title fc-sticky">Some Sample Content here</span></span>
我只接受原始 js 答案,jquery 会被否决。
我有以下字符串,我们称之为 MyHtmlAbove
:
<span class="fc-title-wrap"><span class="fc-title fc-sticky">Some Sample Content here</span></span>
我知道我能做到:
el = document.createElement('div');
el.innerHTML = myHtmlAbove;
我现在不知道怎么办的是:
找到 class fc-title
和内容(前置)和图标标记之前:<i class='font-awesome-here'></i>
我不确定我是否可以只使用普通的 raw:
el.findByClassName('fc-title').prepend('<i class='font-awesome-here'></i> ')
// Result should be:
<span class="fc-title-wrap"><span class="fc-title fc-sticky"><i class='font-awesome-here'></i> Some Sample Content here</span></span>
谁能给我指出正确的方向,我觉得我很接近。
因为我已经创建了一个临时元素来执行此操作,所以我还想清理它,这样它就不会出现在 dom 一些不应该出现的地方。
想法?
document.getElementsByClassName('fc-title')[0].prepend('<i class="font-awesome-here"></i> ')
// Result should be:
// <span class="fc-title-wrap"><span class="fc-title fc-sticky"><i class='font-awesome-her
<span class="fc-title-wrap"><span class="fc-title fc-sticky">Some Sample Content here</span></span>
document.getElementsByClassName('fc-title')[0].prepend('<i class="font-awesome-here"></i> ')
原始 Js() 版本:
const fcT = document.querySelector(".fc-title");
const t = fcT.innerText
const icon = "<i class='font-awesome-here'></i>"
fcT.innerHTML = icon + " " + t;
console.log(document.querySelector(".fc-title"));
<span class="fc-title-wrap"><span class="fc-title fc-sticky">Some Sample Content here</span></span>