Javascript - 更改html标记类型 [英] Javascript - Change html tag type

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

问题描述

我有一个问题,我可以将html标签替换为另一个吗?

i have a question, can i replace html tag to another ?

但我不想将内容留空。
是这样的:

But i don't want to make the content blank. like this:

<a data-text="text">content</a>

<div data-text="text">content</div>

任何想法?

推荐答案

没有。你不能改变的 标签名 nodeName )DOM节点。

No. You cannot change the tagName (nodeName) of a DOM node.

您只能创建所需类型的新节点,复制所有属性(可能还有属性)并移动子节点。然而,你会失去像附加事件监听器这样无法访问的东西。例如,当您想要更改输入的类型时,可以使用此技术IE

You only could create a new node of the wanted type, copy all attributes (and maybe properties) and move the child nodes. Yet you would loose inaccessible things like attached event listeners. This technique is for example used when you want to change the type of an input in IE.

但是,绝对没有理由将 a 更改为 div ,它们具有完全不同的语义(也是行为和布局)。

However, there is absolutely no reason to change an a into a div, they have completely different semantics (also behaviour and layout).

这篇关于Javascript - 更改html标记类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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