使用jquery保持href和html elemens将H3更改为H2 [英] Change H3 to H2 with jquery keeping href and html elemens

查看:227
本文介绍了使用jquery保持href和html elemens将H3更改为H2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 h3 标记更改为 h2 ,并保留所有默认的html元素,代码正在删除所有html内容!!

I need to change (just) the h3 tag to h2, keeping all default html elements inside, this code are erasing all html contents !!

<script type="text/javascript">
$(function () {
        $('.sideMenu h3').replaceWith(function () {
        return "<h2>" + $(this).text() + "</h2>";
    });
});
</script>

<div class="sideMenu">
    <h3 class="sapanos">
       <span></span>
       <a href="#" title="Sainos">Sapanos</a>
    </h3>
</div>


推荐答案

使用 .html() 保留所有的HTML结构。 text()只是获取纯文本,并丢弃所有标签。

Use .html() to keep all the HTML structure. text() just gets the plain text, and throws away all the tags.

$('.sideMenu h3').replaceWith(function () {
    return "<h2>" + $(this).html() + "</h2>";
});

要保留这些类,请执行以下操作:

To keep the classes, do:

$('.sideMenu h3').replaceWith(function() {
    return $("<h2>", {
                "class", this.className,
                html: $(this).html();
            });
});

这篇关于使用jquery保持href和html elemens将H3更改为H2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆