聘我网

新概念招聘3.0

jquery如何实现原地替换?

vote up0vote downstar
<a id="test">test</a>

比如把上面的$('#test')替换成<div>test</div>

 

1 个答复

vote up0vote downcheck

使用replaceWith

$('#test').replaceWith('<div>test</div>');

更新

注意,该函数返回被替换的DOM元素,如果要获得替换后的DOM元素,则需使用replaceAll

Replaces the elements matched by the specified selector with the matched elements. As of jQuery 1.3.2, returns all of the inserted elements. This function is the complement to replaceWith() which does the same task with the parameters reversed.

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.