【发布时间】:2011-08-14 09:48:06
【问题描述】:
我在将 .html 页面(内部带有 JavaScript)加载到 div 元素时遇到问题。
这是我的 div:
<div class="content"></div>
这是我的 php:
<?php
if(!$_POST['page']) die("0");
$page = (int)$_POST['page'];
if(file_exists('pages/page_'.$page.'.html'))
echo file_get_contents('pages/page_'.$page.'.html');
else echo 'There is no such page!';
?>
这是我的脚本:
var default_content="";
$(document).ready(function(){
checkURL();
$('ul li a').click(function (e){
checkURL(this.hash);
});
//filling in the default content
default_content = $('.content').html();
setInterval("checkURL()",250);
});
var lasturl="";
function checkURL(hash)
{
if(!hash) hash=window.location.hash;
if(hash != lasturl)
{
lasturl=hash;
// FIX - if we've used the history buttons to return to the homepage,
// fill the pageContent with the default_content
if(hash=="")
$('.content').html(default_content);
else
loadPage(hash);
}
}
function loadPage(url)
{
url=url.replace('#page','');
$('.content').css('visibility','visible');
$.ajax({
type: "POST",
url: "load_page.php",
data: 'page='+url,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0)
{
$('.content').html(msg);
}
}
});
}
当页面被加载时,.html 页面内的 JavaScript 代码不会被执行。 如果我点击浏览器重新加载按钮,就会执行 JavaScript 代码。
如何使用 jquery 在页面内执行 JavaScript?
【问题讨论】:
标签: javascript jquery html ajax httpresponse