代码如下,我想在class="test"里面的内容每个图片src的地址前加上一个网址比如:src="www.csdn.net/Public/images/bottom1.jpg"><SCRIPT type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></SCRIPT>  
<title>无标题文档</title>
<script type="text/javascript">
    $(document).ready(function () {
        $("button").click(function () {
            $("img").each(function () { //这里不能只用img,因为我网页里面还有其它的图片的,只以这个来历遍的话就不行了。
                //alert($(this).attr("src"))
            });
        });
    });
</script>
</head>
<body>
<button>SRC加上网址</button>
<div class="test">
  <LI class=li_1><IMG alt="" src="Public/images/bottom1.jpg"></LI>
  <LI class=li_2><IMG alt="" src="Public/images/bottom3.jpg"></LI>
  <LI class=li_2><IMG alt="" src="Public/images/bottom2.jpg"></LI>
  <LI class=li_2><IMG alt="" src="Public/images/bottom4.jpg"></LI>
  <LI class=li_2><IMG alt="" src="Public/images/bottom5.jpg"></LI>
  <LI class=li_3><IMG alt="" src="Public/images/bottom6.jpg"></LI>
</div>
</body>
</html>

解决方案 »

  1.   


    <script type="text/javascript">
      $(document).ready(function () {
        $("button").click(function () {
          $(".test li img").each(function(){
            var stringObj=this.src
            $(this).attr("src",stringObj.replace(/Public/, "www.csdn.net/Public"));
          });
        });
      });
    </script>
      

  2.   

    楼上朋友的也是正确的,如果你只是想针对特定的目录做替换,那楼上朋友的就可以了
    但如果你想增加后可以指向其他的网站的话,就需要做点小改动
    $("button").click(function () {
          $(".test li img").each(function(){
                var _src=$(this).attr("src");
                $(this).attr("src","http://www.csdn.net/"+_src);
          });
        });
      

  3.   

    这样会出错,读出来的$(this).attr("src") = "file:///C:/Users/xiaochang/Desktop/Public/images/bottom1.jpg",执行$(this).attr("src","http://www.csdn.net/"+_src);后就会变成“http://www.csdn.net/file:///C:/Users/xiaochang/Desktop/Public/images/bottom1.jpg”