gpt4 book ai didi

php - Ajax页面加载,刷新页面时消失

转载 作者:行者123 更新时间:2023-11-28 02:40:58 25 4
gpt4 key购买 nike

我现在正在使用 Ajax,我有这段代码:

<script type="text/javascript">
function load(file,container)
{
var xmlhttp, newhash = '';

if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest();}
if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}

xmlhttp.onreadystatechange= function (){
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById(container).innerHTML=xmlhttp.responseText;
} else {
document.getElementById(container).innerHTML='<div style="padding:10px"> <p>Please upgrade your web browser to support modern technology.</p></div>';
}
}
xmlhttp.open("GET",file,true);
xmlhttp.send(null);
}
</script>
<script type="text/javascript" src="jquery.js"></script>
<ul>
<li><a href="#" onclick="load('home.php','ajax');return false;">Home</a><li/>
</ul>
<div id="ajax">



</div>

抱歉,这是我的第一篇文章,但我真正关心的是,页面可以加载,但当我刷新页面时,页面就会消失。我尝试在网址上添加哈希值,但仍然不起作用。

最佳答案

您的内容仅在单击按钮时加载。您需要告诉浏览器根据您的哈希值自动加载*内容。

为此,您可以:

1 - 通过在 href 属性中指定,为您的链接提供哈希值。从 onclick 中删除 return false; 以确保页面 URL 更改为 #home

<a href="#home" onclick="load('home.php','ajax');">Home</a>

2 - 添加一个 onload 处理程序,自动加载哈希中指定的页面。

<body onload="loadHash();">

3 - 编写自动哈希加载器

<script>
function loadHash() {
var hash = window.location.href.split('#');
if(hash.length == 2) {
load(hash[1] + '.php','ajax');
}
}
</script>

关于php - Ajax页面加载,刷新页面时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658352/

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