大家好,我正在寻求帮助来重写图像路径。
让我解释一下......
假设我正在运行 2 个应用程序。 APP-A 和APP-B。我的 APP-A 安装了一个插件,可以生成纯 HTML 格式的菜单输出并保存为 HTML 文件。
我现在想将此 HTML 文件导入到我的 APP=B 中并将其用作菜单。 HTML 文件看起来像这样......
<div class="menu">
<li class="item27 parent root" >
<a class="daddy item image subtext" href="/xyz/cms/index.php>
<span>
<img src="tmpl/abc_template/images/icons/icon.png" alt="icon1.png" />
Link 1 <em>subline</em>
</span>
</a>
</li>
<li class="item27 parent root" >
<a class="daddy item image subtext" href="/xyz/cms/index2.php>
<span>
<img src="tmpl/abc_template/images/icons/icon.png" alt="icon2.png" />
Link 2 <em>subline</em>
</span>
</a>
</li>
<li class="item27 parent root" >
<a class="daddy item image subtext" href="/xyz/cms/index3.php>
<span>
<img src="templ/abc_template/images/icons/icon.png" alt="icon3.png" />
Link 3 <em>subline</em>
</span>
</a>
</li>
</div>
我使用 require_once('../../app-a/menu.html'); 将此文件导入到我的 APP-B 中
文件正常,但我遇到图像路径问题。这些路径是相对于 APP-A 生成的,我想更改它们,以便我的 APP-B 从正确的位置获取它们。
例如,我在APP-B中导入文件后,
我希望我的 APP-B 在图像路径之前读取 add ../../,以便这些路径:
<img src="tmpl/abc_template/images/icons/icon.png" alt="icon1.png" />
<img src="tmpl/abc_template/images/icons/icon.png" alt="icon2.png" />
<img src="tmpl/abc_template/images/icons/icon.png" alt="icon3.png" />
变得像这些路径
<img src="../../tmpl/abc_template/images/icons/icon.png" alt="icon1.png" />
<img src="../../tmpl/abc_template/images/icons/icon.png" alt="icon2.png" />
<img src="../../tmpl/abc_template/images/icons/icon.png" alt="icon3.png" />
请帮忙。我正在使用支持 jQuery 的模板。因此,即使是 jQuery 解决方案也可以。
最佳答案
嗯...我一直很喜欢 jQuery 解决方案...
$(function() {
$('.menu img').each(function() {
img_path = $(this).attr('src');
$(this).attr({'src': '../../' + img_path});
});
});
经过测试并有效。
关于php - 如何重写图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4474093/