Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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 更改HTML中任意位置的所有绝对URL_Javascript_Html_Url - Fatal编程技术网

Javascript 更改HTML中任意位置的所有绝对URL

Javascript 更改HTML中任意位置的所有绝对URL,javascript,html,url,Javascript,Html,Url,我有一个带有常规标记的HTML,看起来类似于: <html> <head> ... </head> <body> ...some html.... <script> window.siteRoot = "https://example.com" </script> </body> </html> ..

我有一个带有常规标记的HTML,看起来类似于:

<html>
   <head> ... </head>

   <body> 

        ...some html....

        <script>
           window.siteRoot = "https://example.com"
        </script>

   </body>
</html>

... 
…一些html。。。。
window.siteRoot=”https://example.com"
现在在这个HTML中,我想用
example.com
替换所有绝对URL,作为
/
的域。我怎么能这样做


我知道我可以通过获取锚定标记,然后更改它们的href来更改锚定标记的
href
,但在这里,我想做的是超越锚定标记,找到并替换绝对URL,它可以在上面的
脚本
标记中的任何位置。我怎么能这样做

如果我正确理解了您的问题,您可以循环DOM,使用indexOf()搜索每个元素以查找该url,然后更改它。在循环中使用getElementsByTagName(),或者每个元素都使用indexOf()检查url,并包含一个if语句-it indexOf()>-1,将url重新分配给新的url。

html元素在这里会有所帮助

在这个示例中,我快速添加了一些内联javascript,只是为了演示:
onmouseover=“console.log(this.src)”
,这样我们就可以在鼠标悬停时查看url

但同样,它是纯html。它将应用于文档中的所有相关URL。如果需要为某个特定元素避免使用它,请传入完整的url(

您将看到
img
的src url变为
https://example.com/img.jpg


…一些html。。。。


您可以脱机处理吗?@Mr.希望在页面渲染或已渲染时在浏览器上执行此操作。@Mr.如果我要脱机处理此操作,方法会有什么不同?如果您可以脱机处理文件,那么您可以利用我在问题中给出的示例如何执行此操作?var all=document.getElementsByTagName(“*”;对于(var i=0,max=all.length;i-1{all[i].innerText=newUrl}