【发布时间】:2018-10-13 21:15:27
【问题描述】:
我正在看一个由 Codyhouse (article | demo) 完成的演示,它使用 loadNewContent() 从另一个 HTML 文件中引入内容。一切功能完美,但是 URL 保持不变,始终为 /index.html。
我一直在调整 JS 以尝试使其页面 URL 与内容一起更新,但没有成功。我尝试过使用 replace(),但一直卡在一个循环中……或者删除了一些代码,它根本不起作用。
关于我应该走哪条路线来完成这项工作的任何想法?我希望它按原样运行,但如果您单击“关于”,我希望页面 URL 为 /about.html 等。
function loadNewContent(newSection) {
//create a new section element and insert it into the DOM
var section = $('<section class="cd-section '+newSection+'"></section>').appendTo($('main'));
//load the new content from the proper html file
section.load(newSection+'.html .cd-section > *', function(event){
//add the .cd-selected to the new section element -> it will cover the old one
section.addClass('cd-selected').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
//close navigation
toggleNav(false);
});
section.prev('.cd-selected').removeClass('cd-selected');
});
$('main').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
//once the navigation is closed, remove the old section from the DOM
section.prev('.cd-section').remove();
});
if( $('.no-csstransitions').length > 0 ) {
//detect if browser supports transitions
toggleNav(false);
section.prev('.cd-section').remove();
}
我熟悉 JS,但不是开发人员...所以非常感谢任何帮助!
【问题讨论】:
-
澄清一下,您希望在浏览器地址栏中更新页面 URL 吗?
-
是的,完全正确。现在,页面加载了新内容,但 url 保持不变。我希望它更新到加载的任何页面(about.html、project.html 等)
标签: javascript html url