嗨,我基本上试图通过 php 获取页面,获取其 html 并将 html (以突出显示某些关键字)更改为一点并将其显示为我的页面(jquery)中的覆盖层。
//My php page data.php
<?php
$html= file_get_contents($_GET['url']);
echo $html;
?>
//My jquery ajax request to data.php from page main.html
function test()
{
$.ajax({
type: 'GET',
url: 'data.php',
data: 'url=http://www.developphp.com/view_lesson.php?v=338',
cache: false,
success: function(result)
{
$("#overlay").append(result);
}
});
}
}
如您所见,由于网页使用相对 URL,因此我在叠加层中显示它时遇到问题。我尝试寻找一种将相对值转换为绝对值的方法,但没有找到任何有用的东西。你们能给我指出正确的方法吗?
最佳答案
可以从这里开始
function test(){
var domain='http://www.developphp.com/', path= 'view_lesson.php?v=338';
$.ajax({
type: 'GET',
url: 'data.php',
data: { url: domain + path},
cache: false,
success: function(result)
{
var $html=updatePaths( $(result) );
$("#overlay").append($html);
}
});
}
function updatePaths( $html, domain){
/* loop over all images and adjust src*/
$html.find('img').attr(src,function(i, src){
if(src.indexOf(domain) ==-1){
src= domain+src
}
return src;
})
/* return updated jQuery object*/
return $html;
}
这仅适用于最简单的情况,即远程站点不使用您所使用的域的变体,例如不使用 www
而您却这样做。如果使用 ../
设置图像路径来向上移动目录,也将不起作用。
您必须创建一组更强大的测试来正确操作您使用的最终路径。
我的目的是向您展示如何管理情况
关于php - HTML-PHP 相对路径到绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20030762/