[英]Replace class name <span> tag to <sup> tag
我想将 span class 替换为<sup>
标签
<sup>
<span class="wysiwyg-color-yellow">test</span>
</sup>
见 html,
如果存在<sup>
标签,那么我想像这样将<span>
class 替换为<sup>
标签,
<sup class="wysiwyg-color-yellow">test</sup>
在if条件下
请检查以下示例的 output
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>
在有多个 sup 元素的情况下
const colors = document.querySelectorAll("sup > span"); colors.forEach((color) => { const parent = color.parentNode; parent.innerHTML = color.textContent; parent.classList.add(color.getAttribute("class")); });
<sup> <span class="wysiwyg-color-yellow">test</span> </sup> <sup> <span class="wysiwyg-color-green">test</span> </sup> <sup> <span class="wysiwyg-color-blue">test</span> </sup> <sup> <span class="wysiwyg-color-orange">test</span> </sup>
这可能是最好的解决方案:
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
元素的 class 设置为span
的 class,并将 innerHTML 设置为span
的 innerText。
我希望这是有道理的。
如果您需要 JQuery 库,那么我建议使用jquery-3.2.1.min.js 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.