Logo

替换html的某个标签,使用jQuery将一个标签替换为另一个标签

photo

2022年04月29日

建立在Felix的答案上。

$(‘code’).replaceWith(function() {

var replacement = $(‘

').html($(this).html());

for (var i = 0; i < this.attributes.length; i++) {

replacement.attr(this.attributes[i].name, this.attributes[i].value);

}

return replacement;

});

这将code在替换pre标签中复制标签的属性。

编辑:这将取代甚至code是里面的标签innerHTML等的code标签。

function replace(thisWith, that) {

$(thisWith).replaceWith(function() {

var replacement = $(”).html($(this).html());

for (var i = 0; i < this.attributes.length; i++) {

replacement.attr(this.attributes[i].name, this.attributes[i].value);

}

return replacement;

});

if ($(thisWith).length>0) {

replace(thisWith, that);

}

}

replace(‘code’,’pre’);

所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

热门文章

修复群晖Synology Drive client右键菜单缺失问题 本教程主要解决windows10右键菜单中没有SynologyDrive菜单的问题,整体思路是找到...修复群晖SynologyDriveclient右键菜单缺失问题 作者:Pastore Antonio
1810 浏览量
docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 前言:有时候我们会在docker上下载一个镜像,或者是上传一个镜像到docker上,甚至有时候就是在...docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 作者:Pastore Antonio
1787 浏览量
configure: error: Package requirements (oniguruma) were not met configure:error:Packagerequirements(oniguruma)...configure:error:Packagerequirements(oniguruma)werenotmet 作者:Pastore Antonio
1527 浏览量
Adobe Acrobat Pro 激活 这里记录了一些AdobeAcrobat的激活教程和组件。浏览量:1,679 作者:Pastore Antonio
1523 浏览量
追寻日出,找回自己 为什么我要去追寻日出?其实我是一个很懒的人,每次都起不来,直到有一次我在租房中睡到了大天亮,阳光照...追寻日出,找回自己 作者:Pastore Antonio
1504 浏览量