你如何用jQuery中的另一个标签替换HTML标签? [英] How do you replace an HTML tag with another tag in jquery?
本文介绍了你如何用jQuery中的另一个标签替换HTML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个我正在使用的网站,它使用搁置标签,即使使用HTML5 Shiv,我也无法让IE8阅读,无论我尝试什么。所以,我想知道,你如何用jQuery替换其他标签的现有标签?
例如,如果我想改变 < aside>
< h3>< / h3>
< / aside>
至
< DIV>
< h3>< / h3>
< / div>
如何完成?
解决方案
试试这个:
$('aside')。contents()。unwrap( ).wrap( '< DIV />');
- 将
的内容放在一边
first。 - 现在
unwrap
内容。 - code> wrap 新标签内的内容,这里是
div
。
另外,您可以使用 .replaceWith( )
方法如:
$('aside')。replaceWith(function( ){
return $('< 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 adiv
.
Also, you can do this using .replaceWith()
method like:
$('aside').replaceWith(function () {
return $('<div/>', {
html: $(this).html()
});
});
这篇关于你如何用jQuery中的另一个标签替换HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文