使用jQuery更改HTML标记? [英] Use jQuery to change an HTML tag?

查看:89
本文介绍了使用jQuery更改HTML标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能吗?

示例:

$('a.change').click(function(){
//code to change p tag to h5 tag
});


<p>Hello!</p>
<a id="change">change</a>

因此,单击更改锚点将导致<p>Hello!</p>部分更改为(例如)h5标签,因此单击后最终以<h5>Hello!</h5>结尾.我知道您可以删除p标签并将其替换为h5,但是实际上是否有修改HTML标签的方法?

So clicking the change anchor should cause the <p>Hello!</p> section to change to (as an example) an h5 tag so you'd end up with <h5>Hello!</h5> after the click. I realize you can delete the p tag and replace it with an h5, but is there anyway to actually modify an HTML tag?

推荐答案

一旦创建了dom元素,该标签就是不可变的.您必须执行以下操作:

Once a dom element is created, the tag is immutable, I believe. You'd have to do something like this:

$(this).replaceWith($('<h5>' + this.innerHTML + '</h5>'));

这篇关于使用jQuery更改HTML标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆