下面的代碼非常混亂,它會很整潔,但我希望你能解決你需要的東西。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script language="JavaScript">
$(function(){
$("*", $("#container")).each(function()
{
try
{
//src
if ($(this).attr("src"))
if ($(this).attr("src").lastIndexOf("/")!=-1)
$(this).attr("src", $(this).attr("src").substr($(this).attr("src").lastIndexOf("/")+1));
//href
if ($(this).attr("href"))
if ($(this).attr("rel"))
if ($(this).attr("rel")=="stylesheet")
if ($(this).attr("href").lastIndexOf("/")!=-1)
$(this).attr("href", $(this).attr("href").substr($(this).attr("href").lastIndexOf("/")+1));
}
catch(ex)
{
$("#lstError").append("<div>"+ex.description+"</div>");
}
});
});
</script>
</head>
<body>
<div id="lstError"></div>
<div id="container">
<!-- your html -->
<link rel="stylesheet" href="pepe/all.css">
<img src="lele/bl.png" />
<img src="lele/b.png" />
</div>
</body>
</html>
爲什麼?爲什麼在JavaScript中? – 2009-10-21 20:47:42
,因爲我想在本地保存html,並且能夠在沒有連接的情況下正確查看頁面。創建相同的目錄結構(特別是如果外部文件位於不同的域中)是站不住腳的,所以我需要「扁平化」目錄結構。 – cannyboy 2009-10-21 20:54:10
我知道,至少在Firefox中,你可以做文件 - >保存頁面,然後選擇「另存爲類型:網頁,完成」,Firefox將下載本地的所有js/css/img文件並更新HTML文檔您。否則,JavaScript不是這樣做的正確方法。 – Tinister 2009-10-21 21:24:37