Jquery 用新标记替换HTML标记
如何使用jQuery将Jquery 用新标记替换HTML标记,jquery,Jquery,如何使用jQuery将html标记替换为新标记 我有许多table标记,我想替换包含类的特定表。它的所有子节点也应该被替换 <table class='replace_this'> <tr> <td> All table, tr, and td tag are replaced with div tag </td> </tr> </table> 所有
html
标记替换为新标记
我有许多table
标记,我想替换包含类的特定表。它的所有子节点也应该被替换
<table class='replace_this'>
<tr>
<td>
All table, tr, and td tag are replaced with div tag
</td>
</tr>
</table>
所有表格、tr和td标签均替换为div标签
应改为:
<div class='replace_this'>
<div>
<div>
All table, tr, and td tag are replaced with div tag
</div>
</div>
</div>
所有表格、tr和td标签均替换为div标签
我有这样的想法:
$.each(['td', 'tr', 'table'], function(index, value){
$(value).replaceWith(function() {
return '<div>' + $(this).html() + '</div>';
});
});
$。每个(['td','tr','table'],函数(索引,值){
$(值).替换为(函数(){
返回“”+$(this).html()+“”;
});
});
代码:您必须从内到外开始:
$('.replace_this td').each(function() {
$(this).replaceWith(function() {
return $('<div>' + this.innerHTML + '</div>')
})
});
$('.replace_this tr').each(function() {
$(this).replaceWith(function() {
return $('<div>' + this.innerHTML + '</div>')
})
});
$('table.replace_this ').each(function() {
$(this).replaceWith(function() {
return $('<div class="replace_this">' + this.innerHTML + '</div>')
})
});
$('.replace_this td')。每个(函数(){
$(this).replaceWith(function(){
返回$(''+this.innerHTML+'')
})
});
$('.replace_this tr')。每个(函数(){
$(this).replaceWith(function(){
返回$(''+this.innerHTML+'')
})
});
$('table.replace_this')。每个(函数(){
$(this).replaceWith(function(){
返回$(''+this.innerHTML+'')
})
});
该死的迈克,写小提琴的过程中有四分之三是这样的:)很好,但他只要求特定的课程;)<顶部
div
元素中缺少code>class属性。一个问题是,如何选择div中的文本并将其包装在“”标记中?返回$(“”+this.innerHTML+””)
只需添加一个p标记class
属性在顶部div
元素中缺少。