如何在 jquery 中用另一个标签替换 HTML 标签?
我有一个正在开发的网站,它使用aside"标签,无论我尝试什么,我都无法让 IE8 能够读取,即使使用 HTML5 Shiv 也是如此.所以,我想知道,你将如何使用 jQuery 将现有标签替换为其他标签?
例如,如果我想改变
<h3></h3></旁白>到
<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
asidefirst. - Now
unwrapthe contents. - Now simply,
wrapthe contents inside a new tag, here adiv.
DEMO
Also, you can do this using .replaceWith() method like:
$('aside').replaceWith(function () {
return $('<div/>', {
html: $(this).html()
});
});
DEMO
相关文章