如何用另一个标签替换标签并使用 jquery 保留属性?

How to replace a tag with another and keep attributes using jquery?

如何将<ins>标签替换为<del>标签,并将其属性保留在javascript或jquery中?像下面这样

<ins data-author="auth1" data-ins-username="user1">auth1 text</ins>
<!-- After -->
<del data-author="auth1" data-ins-username="user1">auth1 text</del>

我发现了类似的问题和答案,但它不保留属性。

您可以使用 jquery .replaceWith() 将标签替换为另一个。在函数中获取元素的 outerHTML 并将标记名称替换为另一个。

$("ins").replaceWith(function(){
    return this.outerHTML.replace("<ins", "<del").replace("</ins", "</del")
});

$("ins").replaceWith(function(){
    return this.outerHTML.replace("<ins", "<del").replace("</ins", "</del")
});
console.log($("del")[0].outerHTML);
del { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ins data-author="auth1" data-ins-username="user1">auth1 text</ins>