如何在 jquery 中用另一个标签替换 HTML 标签? [英] How do you replace an HTML tag with another tag in jquery?

查看:29
本文介绍了如何在 jquery 中用另一个标签替换 HTML 标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在开发的网站,它使用aside"标签,无论我尝试什么,我都无法让 IE8 能够读取,即使使用 HTML5 Shiv 也是如此.所以,我想知道,你将如何使用 jQuery 将现有标签替换为其他标签?

例如,如果我想改变

<h3></h3></div>

那该怎么做?

解决方案

试试这个:

$('aside').contents().unwrap().wrap('<div/>');

  • 先获取aside的内容.
  • 现在解开内容.
  • 现在简单地说,wrap 将内容放入一个新标签中,这里是 div.

演示

<小时>

此外,您可以使用 .replaceWith() 方法来执行此操作,例如:

$('aside').replaceWith(function () {返回 $('<div/>', {html: $(this).html()});});

演示

I have a site I'm working on and it uses 'aside' tags, which I'm not getting IE8 to be able to read no matter what I try, even with an HTML5 Shiv. So, I'm wondering, how would you replace existing tags with other tags with jQuery?

For example, if I wanted to change

<aside>
  <h3></h3>
</aside>

to

<div>
  <h3></h3>
</div>

How would that be done?

解决方案

Try this:

$('aside').contents().unwrap().wrap('<div/>');

  • Get the contents of aside first.
  • Now unwrap the contents.
  • Now simply, wrap the contents inside a new tag, here a div.

DEMO


Also, you can do this using .replaceWith() method like:

$('aside').replaceWith(function () {
    return $('<div/>', {
        html: $(this).html()
    });
});

DEMO

这篇关于如何在 jquery 中用另一个标签替换 HTML 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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