如何将 Window.Location.Hash 与数组中包含的列表进行比较以确定“当前位置”?
var exampleArray = ["#home", "#support", "#about"];
页面上还有其他几个 anchor ,但我只希望这些 anchor 更改导航 Pane 中的“当前位置”。
最佳答案
最简单的可能是使用Array.prototype.indexOf
,如下所示:
var hash = window.location.hash,
anchorList = ['#index', '#support'];
if( anchorList.indexOf( hash ) > -1 ){
// anchor is in list, do your thing ...
}
不幸的是,这仅适用于 IE9 及更高版本(显然还适用于 Chrome、SF、FF)。如果您必须支持 IE8(甚至更低),您可以尝试使用 polyfill、for 循环或使用 http://underscorejs.org/它有一个 _.indexOf
帮助器。
关于javascript - 将 Location.Hash 与数组中包含的列表进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330448/