jQuery:如何更改标签名称? [英] jQuery: how to change tag name?

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

问题描述

jQuery:如何更改标签名称?



例如:

 < TR> 
$ 1
< / tr>

我需要

 code>< DIV> 
$ 1
< / div>

是的,我可以


  1. 创建DOM元素< div>

  2. 将tr内容复制到div

  3. 从dom中删除tr

但是我可以直接使用吗?



PS:

  $(tr).get(0).tagName =div; 

导致 DOMException

解决方案

您可以使用jQuery的 .replaceWith()方法替换任何HTML标记。 / p>

示例: http://jsfiddle.net/JHmaV/



参考:。替换为



如果要保留现有标记,可以使用以下代码:

 code> $('#target')。replaceWith('< newTag>'+ $('target')。html()+'< / newTag>')


jQuery: how to change tag name?

For example:

<tr>
    $1
</tr>

I need

<div>
    $1
</div>

Yes, I can

  1. Create DOM element <div>
  2. Copy tr content to div
  3. Remove tr from dom

But can I make it directly?

PS:

    $(tr).get(0).tagName = "div"; 

results in DOMException.

解决方案

You can replace any HTML markup by using jQuery's .replaceWith() method.

example: http://jsfiddle.net/JHmaV/

Ref.: .replaceWith

If you want to keep the existing markup, you could use code like this:

$('#target').replaceWith('<newTag>' + $('target').html() +'</newTag>')

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

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