30秒到达战场
建立在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');