尝试 jQuery '加载':
示例代码:
<style>
#content{
border:1px solid #000;
padding:10px;
height:160px;
width:230px;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type='text/javascript'>
$(function(){
$('button').click(function(){
var btnId = $(this).attr('id');
$("#content").load(btnId + ".html", function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
alert(msg + xhr.status + " " + xhr.statusText);
}
});
});
})
</script>
<button id="index">Index</button>
<button id="welcome">Welcome</button>
<button id="contact">Contact</button>
<div id="content"></div>
这是 HTML 文件:
- index.html 的内容
'<hi>index page</hi>'
- 带有内容的welcome.html
'<hi>welcome page</hi>'
- 带有内容的contact.html
'<hi>contact page</hi>'
现在这里是
使用 PHP 的外部 url 加载示例:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type='text/javascript'>
$(function(){
$('button').click(function(){
// get url from text box
var url = $('#url').val();
$("#content").load('process.php?url='+ url, function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
alert(msg + xhr.status + " " + xhr.statusText);
}
});
});
})
</script>
<input type="text" name="url" id="url" value='' /><button id="load">Load URL Page</button>
<div id="content"></div>
PHP代码:
<?php
$homepage = file_get_contents($_GET['url']);
echo $homepage;
?>