1. 程式人生 > >關於HTML檔案顯示不出來的情況

關於HTML檔案顯示不出來的情況

錯誤的程式碼:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<base href="//www.w3cschool.cn/" target="_blank">  
</head>
<body style="background-color:yellow;">
	<a href="//www.w3cschool.cn/">Visit W3CSchool!</a>
	<a id="tips">Useful Tips Section</a>
	<a href="#">Visit the Useful Tips Section</a> 
	 <p style="color:blue;margin-left:20px;">This is a paragraph.</p> 
	 <h2 style="background-color:red;">This is a heading</h2>
	 <p style="background-color:green;">This is a paragraph.</p>
	 <div><img src="./images/ball.png" alt="ball"/></div>
</body>
</html>

實驗結果:


what ****k?

查了很久一直沒找到頭緒,後來發現是base的問題。。。。

<base href="//www.w3cschool.cn/" target="_blank">  

弄懂這個問題首先要理解這行程式碼的意思:


<a href="#">Visit the Useful Tips Section</a> 

這個程式碼會在下面的路徑前面加上base的路徑,所以

 <div><img src="./images/ball.png" alt="ball"/></div>

當我想輸出這個ball的時候,不會再我的本地查,會直接在w3c網站找,那麼路徑當然是有問題的啦~~~大笑

正確的姿勢是:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body style="background-color:yellow;">
	<a href="//www.w3cschool.cn/">Visit W3CSchool!</a>
	<a id="tips">Useful Tips Section</a>
	<a href="#">Visit the Useful Tips Section</a>
	 <p style="color:blue;margin-left:20px;">This is a paragraph.</p> 
	 <h2 style="background-color:red;">This is a heading</h2>
	 <p style="background-color:green;">This is a paragraph.</p>
	 <div><img src="./images/ball.png" alt="ball"/></div>
</body>
</html>

刪去這個base的預設頭