使用jQuery更改元素的类型

Dav*_*ard 4 html javascript jquery

如果我知道元素的ID,是否可以更改它具有的HTML标记的类型?

例如,如果你有<p id="p">Lots and lots of text here.</p>,是否可以将其更改为<span id="p">....

谢谢.

Jam*_*xon 9

您可以使用该replaceWith方法执行此操作.您需要重建替换的属性.

$('#p').replaceWith(function(){
   return '<span>' + $(this).contents().text() + '</span>';
});
Run Code Online (Sandbox Code Playgroud)

工作实例