更改节点类型 [英] Change node type

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

问题描述

使用jQuery可以将所有匹配的元素更改为不同的类型?例如,我可以选择所有 a 标签( $(a))并将其更改为输入 s?

Using jQuery, it possible to change all matched elements to a different type? For example, can I select all a tags ($("a")) and change them to inputs?

推荐答案

不,你实际上不能改变它,但你可以使用 replaceWith() 替换新元素[docs] 方法。

No, you can't actually change it, but you can replace them with a new element using the replaceWith()[docs] method .

$("a").replaceWith("<input>");

示例: http://jsfiddle.net/7mAtX/

如果您想保留任何属性,则需要手动设置它们。

If there are any attributes that you want to keep, you'll need to manually set them.

$("a").replaceWith(function() {
    return $("<input>",{'class':this.className,
                         value:this.innerHTML
                       });
});

示例: http://jsfiddle.net/7mAtX/1/

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

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