change
事件变量通过以下属性传递给函数: value, path, pathNames, parameterNames, parameters, queryString
。您要监视的属性是pathNames
。
以下是我整理的一些片段,可以帮助您跟踪您的深度以及到底发生了什么变化:
var $current_path = window.location.hash;
$.address.change( function(event) {
// get the difference in the two paths
$changed_path = event.path.replace(new RegExp('^'+$current_path,'i'), '');
// make sure we update the current path
$current_path = event.path;
// how deep is the new path?
$level = event.pathNames.length;
// break the changed part into segments, ignoring leading/trailing slashes
$changed_path_array = $changed_path.replace(/^\/|\/$/g, '').split('/');
// let's see what level actually changed from the current path
$changed_level = $level - $changed_path_array.length;
} );
然后,您可以通过将新深度与段数组结合使用来组织函数的其余部分,以准确定位需要更新的内容。根据 $current_path,您可能正在执行新的页面加载或页面某处的微小更改。