我要将span类替换为标记
<sup>
<span class="wysiwyg-color-yellow">test</span>
</sup>
请参阅html,
如果存在标记,则我希望将
类替换为
标记,
<sup class="wysiwyg-color-yellow">test</sup>
在if条件下
这里可能是最好的解决方案:
var buttons = $("sup");
for(var i=0; i< buttons.length; i++){
if (buttons[i].children[0]) {
if (buttons[i].children[0].nodeName == "SPAN") {
var spanElement = buttons[i].children[0];
buttons[i].setAttribute("class", spanElement.getAttribute("class"));
buttons[i].innerHTML = spanElement.innerText;
}
}
}
它选择每个sup
元素,并检查其中是否有span
。 如果存在span
元素,则将sup
元素的类设置为span
的类,并将innerHTML设置为span
的InnerText。
我希望这说得通。
如果您需要JQuery库,那么我建议使用jquery-3.2.1.min.js。
请检查以下示例的输出
null
const color = document.querySelector(".wysiwyg-color-yellow");
const sup = document.querySelector("sup");
if (color.nodeName === "SPAN") {
sup.innerHTML = color.textContent; // here is removed the inner span
sup.classList.add(color.getAttribute("class"));
}
<sup>
<span class="wysiwyg-color-yellow">test</span>
</sup>