如题,用js向php发送一个请求并加载响应到当前页面的时候,如何避免重写是覆盖当前页面;
具体情况是这样的:
js:
var navLst = {};
var reqInfo = {};
reqInfo['reqNav'] = true;
reqInfo['reqSin'] = 'qtrfss';
//more limt options would be added
var reqSent= JSON.stringify(reqInfo);
var xhr = null;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = ActiveXObject('Microsoft.XMLHttp');
}
xhr.open('POST', '/themes/ticket/model/echoNav.php',true);
xhr.onreadystatechange=function(){
if(xhr.readyState == 4) {
if(xhr.status == 200) {
try{
var resp = eval('('+ xhr.responseText +')');
listMenu(resp);
}catch(e){
listMenu(e);
}
} else {
//undo
}
}
};
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('reqSent='+reqSent);
/themes/ticket/model/echoNav.php返回的是一个数组字符串化的json;
然后获取到了menlist,重写的时候整个页面只剩下menulist了。。。
listmenu:
function listMenu(menuLst){
//var menuLst = ['Sci-Fi','home','wiki'];//menu lst should come from php
var nav = '';
var navIE = ''+
''+
''+
''+
''+
''+
''+
''+
''+
''+
' '+
''+
'';
var navStyle = '';//stylesheet's url
nav +=navIE;
nav +=navStyle;
nav +='';
document.write(nav);
var navUl = document.createElement('ul');
navUl.id = 'onav_bar';
var navLst = ''
for (var i=0 ;i <p><br></p><p>解决方案:</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p><br></p><p>是 <code>document.write(nav);</code> , DOM加载完成后再用<code>document.write()</code>方法会覆盖以前内容。测试一下,把其中函数中的方法换成<code>document.write('')</code></p><p class="answer fmt"><br></p><p><br></p>
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










