我有一段这样的代码
<p style="position:absolute;top:-866px;left:1550px;white-space:nowrap" class="xd">some text
<p style="position:absolute;top:-866px;left:1550px;white-space:nowrap" class="xd">some other text
我需要用
替换它<h3>some text</h3>
<h3>some other text </h3>
感谢任何帮助。
最佳答案
jquery 解决方案:JSFiddle Demo
$('p[class="xd"]').each(function(index,item){
$(item).replaceWith("<h3>"+$(item).html()+"</h3>"); // keep the text, swap the tags
});
感谢Vishal Taj PM用于显示较短的解决方案:JSFiddle Demo
(对于这种情况,text()
和 html()
之间没有区别)
$('p.xd').each(function(){
$(this).replaceWith("<h3>"+$(this).text()+"</h3>")
});
关于javascript - 如何替换 HTML 标签和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43060171/