How do you replace an HTML tag with another tag in jquery?(如何在 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
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 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 jquery 中用另一个标签替换 HTML 标签?


基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01