0
我有一个标签系统,用于在我的页面上切换两个div
。它运行良好,直到我决定使其与URL一起工作(更具体地说,当onhashchange
事件发生时作出反应)。它只是不时地工作。我不知道问题出在哪一小部分代码中,它让我发疯。哈希变化事件不时起作用
$(function() {
if ("onhashchange" in window) {
console.log("onhashchanged is supported");
} else {
console.log("onhashchanged is not supported")
}
window.onhashchange = function() {
var hash = (window.location.hash.substr(1) != "") ? window.location.hash.substr(1) : "articles";
var tabId = hash + "t";
var tabBtnId = hash + "tb";
$("div.tabbar > div.tabbarButton").removeClass("current");
$("div.tabbar > div.tabbarButton#" + tabBtnId).addClass("current");
var $tabs = $(".tabsContent");
$tabs.find(".tab").removeClass("current");
$tabs.find(".tab#" + tabId).addClass("current");
};
$("div.tabbar > div.tabbarButton").click(function() {
var selectedTabId = $(this).prop("id");
var hashVal = "#" + selectedTabId.substr(0, selectedTabId.length - 2);
window.location.assign(hashVal);
console.log(hashVal);
});
});
我的猜测:我想之前DOM已准备就绪,该功能没有在我的代码调用(因为它位于$(function(){ /*...*/ })
),但发生hashchange
事件,如果是这样的原因,然后我可以在哪里放置事件处理程序,以便它可以与jQuery的操作一起使用?