我把这段js插入到源码中.然后
<img src="http://www.xxx.com/old/logo.gif " />
还是无法显示.logo.gif的目录现在为new

解决方案 »

  1.   

    <script language="javascript" type="text/javascript"> 
    var str = '这里应该是网页的所有源代码';
    var reg = /http:\/\/www.xxx.com\/old\//gi 
    alert(str.replace(reg ,'http://www.xxx.com/new/' )); 
    </script> str必须是网页的的所有源代码,而且不能只替换old,应该考虑有可能网页中有old这个单词,应该上面的代码能够保证只替换超链接中的old。我觉得用JS不是个好方法,因为这样一来每次打开网页都会用JS处理一次,这样会降低网页速度。建议:用Perl或者PHP或者其它方法,将所有网页文件读入,然后用正则表达式替换所有链接,然后输出新的文件,这样比较好。
      

  2.   

    我的问题是我把这段JS插入直接没反映啊.而old实际上是多极目录如 2/3/
    不存在和其他字符重复的问题
      

  3.   

    JS的话,试试这样啊
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <img src="http://www.google.cn/options/icons/blogsearch.gif" />
    <img src="http://www.google.cn/options/icons/blogsearch.gif" />
    <script type="text/javascript">
    window.onload = function()
    {
    var imgs = document.images;
    for(var i=0;i<imgs.length;i++)
    imgs[i].src = imgs[i].src.replace(/blogsearch/ig,"toolbar");
    }
    </script>
    </body>
    </html>
      

  4.   


    没有反应的原因:你写的JS只是用来操作字符串的,而没有对DOM的属性进行修改,5楼的方法是正确的,用Javascript修改所有的<img>元素的src属性的值。
    按照你的要求可以将上面的代码修改如下:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    </head>
    <body>
    <img src="http://www.xxx.com/old/logo1.gif " />
    <img src="http://www.xxx.com/old/logo2. gif " />
    <script type="text/javascript">
    window.onload = function()
    {
        var imgs = document.images;
        for(var i=0;i<imgs.length;i++)
            imgs[i].src = imgs[i].src.replace(/old/ig,"new");
    }
    </script>
    </body>
    </html>
      

  5.   

    恩.可以用.还是有个疑问
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    </head>
    <body>
    <img src="/DownFiles/Book/0/622/2008/11/4/20081104083750395.gif" align="center" border=0>
    <script type="text/javascript">
    window.onload = function()
    {
        var imgs = document.images;
        for(var i=0;i<imgs.length;i++)
            imgs[i].src = imgs[i].src.replace(/\/DownFiles\//ig,"http:\/\/8kk8.net\/DownFiles\/");
    }
    </script>
    </body>
    </html>
    这样的话.图片还是打不开.显示的图片地址怎么是http://xxx.com/DownFiles/Book/0/622/2008/11/4/20081104083750395.gif
    实际的网站地址被附加到了图片地址前面..
    这怎么解决?
      

  6.   

    http://xxx.comhttp://8kk8.net/DownFiles/Book/0/622/2008/11/4/20081104083750395.gif
      

  7.   

    imgs[i].src.replace(/^.*?\/DownFiles\//ig,"http:\/\/8kk8.net\/DownFiles\/");
      

  8.   

    改为这样<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    </head>
    <body>
    <img src="/DownFiles/Book/0/622/2008/11/4/20081104083750395.gif" align="center" border=0>
    <script type="text/javascript">
    window.onload = function()
    {
        var imgs = document.images;
        for(var i=0;i<imgs.length;i++)
            imgs[i].src = imgs[i].src.replace(/.*DownFiles\//ig,"http:\/\/8kk8.net\/DownFiles\/");      
    }
    </script>
    </body>
    </html>