提问者:小点点

将类名<span>标记替换为<sup>标记


我要将span类替换为标记

<sup>
  <span class="wysiwyg-color-yellow">test</span>
</sup>
    

请参阅html,

如果存在标记,则我希望将类替换为标记,

<sup class="wysiwyg-color-yellow">test</sup>

在if条件下


共2个答案

匿名用户

这里可能是最好的解决方案:

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>