简体   繁体   English

根据网址更改页面内容

[英]Change page content according to url

I have a backend that handles requests like 我有一个可以处理类似请求的后端

http://mysite/A.html // return A.html
http://mysite/B.html // return B.html
http://mysite/C.html // return C.html

Now I need to add site menu to visit A,B,C pages from it without page reloading. 现在,我需要添加站点菜单来从中访问A,B,C页面,而无需重新加载页面。 And If I load page A.html, then click on B in the menu, page B should be displayed and url becomes http://mysite/A.html#B . 并且,如果我加载页面A.html,然后单击菜单中的B,则应该显示页面B,并且网址变为http://mysite/A.html#B

So I need somehow write code to allow user to make such requests: 因此,我需要以某种方式编写代码以允许用户发出此类请求:

 http://mysite/A.html // show A.html
 //...same for B.html, C.html
 http://mysite/A.html#B // show B.html
 http://mysite/C.html#A // show A.html
 //...etc

As far as I know PHP backend can't handle # in the url, so I need to use Js. 据我所知,PHP后端无法处理url中的#,因此我需要使用Js。 So guys, any ideas here? 伙计们,这里有什么想法吗?

Currently If I handle request http://mysite/A.html#B , I load A.html from PHP backend then manually replace page content with B.html using JQuery. 当前,如果我处理请求http://mysite/A.html#B ,则从PHP后端加载A.html,然后使用JQuery手动将页面内容替换为B.html。

PS: My current method shows page A.html for a sec before replacing itself to B.html which is bad for me. PS:在将自身替换为B.html之前,我当前的方法显示A.html页面一秒钟,这对我来说是不利的。

You could use the jQuery Ajax GET method to return the HTML contents of another page. 您可以使用jQuery Ajax GET方法返回另一个页面的HTML内容。

jQuery jQuery的

$('a').on('click', function(e) {
e.preventDefault();
var ID = $(this).attr('href');
$.get(ID, function(data) {
$('#MyReturnDiv').html(data);
});
});

HTML 的HTML

<a href="directory of return/A.html">A</a>
<a href="directory of return/B.html">B</a>
<a href="directory of return/C.html">C</a>

<div id="MyReturnDiv"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM