gpt4 book ai didi

javascript - 用平面目录引用替换所有对外部文件(图像、CSS 和 JavaScript)的引用?

转载 作者:行者123 更新时间:2023-11-28 13:51:38 27 4
gpt4 key购买 nike

我正在尝试找出是否有一种方法(使用 JavaScript 或 jQuery)遍历整个 HTML 文件,替换对外部文件的所有引用,并将对其他目录中文件的所有引用替换为相同的文件命名文件,但没有目录信息。

例如,在 HTML 文本中:

  • scripts/script.js 被替换为 script.js
  • images/big.jpg 被替换为 big.jpg
  • css/style.css 被替换为 style.css

等等

我想 CSS 文件中的引用也需要更改。

最佳答案

下面的代码很乱,写的会很整洁,希望大家解决问题。

<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 - 用平面目录引用替换所有对外部文件(图像、CSS 和 JavaScript)的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1603508/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com