【问题标题】:How to replace a tag with another and keep attributes using jquery?如何用另一个标签替换一个标签并使用jquery保留属性?
【发布时间】:2016-09-16 07:16:54
【问题描述】:

如何用<del> 标记替换<ins> 标记并将其属性保留在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>

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

【问题讨论】:

标签: javascript jquery html tags replacewith


【解决方案1】:

您可以使用 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>

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 2013-04-23
    • 2015-06-24
    • 2013-11-23
    • 2015-09-19
    • 2015-12-22
    • 1970-01-01
    相关资源
    最近更新 更多