Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用replaceWith gives";“不是一个函数”;错误(将文本节点替换为文本和html的混合)_Javascript_Jquery_Html - Fatal编程技术网

Javascript 调用replaceWith gives";“不是一个函数”;错误(将文本节点替换为文本和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" },

我有多个文本节点,我想用html替换其中的一些文本:

<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.
})