Javascript 调用replaceWith gives";“不是一个函数”;错误(将文本节点替换为文本和html的混合)
我有多个文本节点,我想用html替换其中的一些文本:Javascript 调用replaceWith gives";“不是一个函数”;错误(将文本节点替换为文本和html的混合),javascript,jquery,html,Javascript,Jquery,Html,我有多个文本节点,我想用html替换其中的一些文本: <pre>{ "data": [ { "source": "<a href=www.e.com>e.com</a>", "created_time": "2015-11-13T06:16:09+0000", "id": "913680432051814" },
<pre>{
"data": [
{
"source": "<a href=www.e.com>e.com</a>",
"created_time": "2015-11-13T06:16:09+0000",
"id": "913680432051814"
},
{
"source": "<a href=www.e.com>e.com</a>",
"created_time": "2015-11-13T06:16:02+0000",
"id": "913680355385155"
},
{
"source": "<a href=www.e.com>e.com</a>",
"created_time": "2015-11-13T06:16:00+0000",
"id": "913680332051824"
}
</pre>
尝试替换任何元素上的。我正在尝试使用x[0]
x[0].replaceWith(rep)
它说:x[0]。替换为非函数
那么,如何用html元素替换文本呢?我尝试了下面的方法,效果很好。使用$(这个)有效
x=$(“pre”)
.contents()
.filter(函数(){
返回this.nodeType==3
})
.each(函数({
rep=this.data.replace(/\“id\”:\“(.*)\”,/,“/\“id\”:\”)
$(this).replaceWith(rep);//当我使用$(this)时,它工作了。
})
rep = x[0].data.replace(//\"id\": \"(.*)\",/, "/\"id\": \"<a href=\"https:example.com/\$1\">$1</a>\"")
x[0].replaceWith(rep)
x=$("pre")
.contents()
.filter(function() {
return this.nodeType === 3
})
.each(function(){
rep = this.data.replace(//\"id\": \"(.*)\",/, "/\"id\": \"<a href=\"https:example.com/\$1\">$1</a>\"")
$(this).replaceWith(rep); //when i used $(this) it worked.
})