javascript - 如何替换 HTML 标签和属性

标签 javascript php jquery css html

我有一段这样的代码

<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/

相关文章:

php - PHP 中的 fatal error 是什么意思?

php - 如何确定 Pheanstalk 作业是否已完成

jquery - Grails中的Ajax驱动选择不刷新选择

javascript - js调整图片大小

javascript - jQuery UI slider : access options from function associated with the slide event

javascript - 如何在jquery中选择预览选择器

javascript - 使用 AngularJS 路由器加载 404 状态

javascript - 了解 javascript 中 forEach 函数语句的语法

php - 创建 QueryBuilder 以查找字段 ManuToMany

javascript - jQuery 使用输入数组进行验证