相对路径的问题

javascript是不允许访问本地文件的,安全问题 你直接双击代开,走的是本地路径,肯定会"拒绝访问" 
你要明白xmlhttp.open("get","01.txt",true),是要让javascript访问同域下的01.txt,
也就是服务器上的文件可以访问,所以在localhost下运行无误

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>无标题文档</title>
 6 <script>
 7 window.onload = function (){
 8     var oImg = document.getElementById('img1');
 9     
10     oImg.onclick = function (){
11         // alert( oImg.src );
12         // img/1.jpg
13         // file:///C:/Users/Administrator/Desktop/11-4-JS1/img/1.jpg
14         // if( oImg.src == 'img/1.jpg' ){
15         // 不能做为判断条件:
16         
17         // 1、所有的相对路径地址
18         /*
19                     img src
20                     href 
21                     
22              2、颜色值:color: red     #f00 rgb() rgba()
23         */
24             
25         if( oImg.src == 'file:///C:/Users/Administrator/Desktop/11-4-JS1/img/1.jpg' ){
26             oImg.src = 'img/2.jpg';
27         }
28     };
29 };
30 </script>
31 </head>
32 
33 <body>
34 
35 <img id="img1" src="img/1.jpg" width="400" />
36 
37 </body>
38 </html>
示例代码
 
原文地址:https://www.cnblogs.com/123wyy123wyy/p/6886588.html