当我输入url值时,我无法更改标题不会更改,并且第一个内容不会隐藏如何使用enter with url更改标题并隐藏第一页内容?
<script type="text/javascript">
$(function() {
var pagecontent = $(".page_content");
var pages = $("ul.pages li,#logo");
var hash = window.location.hash;
var title = document.title;
pagecontent.not(hash).hide();
pages.first().addClass("active").show();
pagecontent.first().show();
pages.find('[href=' + hash + ']').addClass('active');
pages.find('[href=' + hash + ']').parent().closest('li').addClass('active');
pages.click(function() {
$(this).addClass('active').siblings().removeClass('active');
$(this).parent().closest('li').addClass('active').siblings().removeClass('active');
pagecontent.hide();
var activepage = $(this).find("a").attr("href");
$(activepage).fadeIn();
title = $('h2', activepage).first().text();
$('title').text(title);
return false;
});
});
</script>
当我尝试输入网址时,不会更改标题和pagecontent.first保留在那里
答案 0 :(得分:0)
固定。这是:
$(function() {
var pagecontent = $(".page_content");
var pages = $("ul.pages li,#logo");
var hash = window.location.hash || "#home";
var title = document.title;
// new block
var titlex = $('h2', hash).first().text();
document.title = titlex;
$(hash).show();
$('ul.pages a[href="' + hash + '"]')
.addClass("active")
.closest("li")
.addClass("active");
// end
pagecontent.not(hash).hide();
pages.click(function() {
$(this).addClass('active').siblings().removeClass('active');
$(this).parent().closest('li').addClass('active').siblings().removeClass('active');
pagecontent.hide();
var activepage = $(this).find("a").attr("href");
window.location.hash = activepage; //new: fix location hash
$(activepage).fadeIn();
title = $('h2', activepage).first().text();
$('title').text(title);
return false;
});
});
我们可以讨论这种风格,但这很有效。
编辑:请注意click
处理程序设置适当的位置哈希。似乎是正确的决定,但它会导致浏览器标题栏中的闪烁。如果它困扰你,请尝试替换
window.location.hash = activepage;
$('title').text(title);
与
if( history.pushState ) {
history.replaceState({}, title, activepage);
} else {
window.location.hash = activepage;
document.title = title;
}