Internet explorer 使用IE 9进行更改

Internet explorer 使用IE 9进行更改,internet-explorer,hashchange,Internet Explorer,Hashchange,我有以下代码 $(document).ready(function() { if ("onhashchange" in window) { alert("The browser supports the hashchange event!"); } function test(){ alert("hash has changed = " + window.location.hash) } window.onhashchange =test; } 我

我有以下代码

$(document).ready(function() {
   if ("onhashchange" in window) {
      alert("The browser supports the hashchange event!");
   }
   function test(){
  alert("hash has changed = " + window.location.hash)
   }
   window.onhashchange =test;
}
我单击更改哈希的链接,在所有其他浏览器中,我在
test

然而,在IE中,我得到第一个警告,说它支持onhashchange,但当hash更改时,什么也没有发生


有什么想法吗?

MSDN文档上有一个例子

基本上,我删除了他们页面上所有额外的“地图”内容,他们和您的示例之间的唯一区别是他们包含以下元标记:

<meta http-equiv="X-UA-Compatible" content="IE=8" >

在您的示例中,我将此添加到head标记中,效果很好

这个meta标签基本上是说,运行页面就像它在IE8中一样,而不是IE9(仍处于测试阶段)


有关此元标记的更多信息,请阅读当前所有浏览器都支持HTML5中的新hashchange事件;没有必要愚弄你的浏览器以为它是IE8

此代码适用于Win 7上的IE 9、FF 5、Safari 5和Chrome 12:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script>
            window.onhashchange = doThisWhenTheHashChanges;

            function changeTheHash()
            {
                var newHashValue = document.getElementById("newHashInput").value;
                var currentLocation = window.location.pathname;
                window.location.hash = newHashValue;
            }

            function doThisWhenTheHashChanges()
            {
                alert("The hash has changed!");
            }
        </script>
    </head>
    <body> 
        <h1>Hash Change Test</h1>
        <form>
            <input type="text" id="newHashInput" autofocus>
            <input type="button" value="Set" onclick="changeTheHash()">
        </form>
    </body>
</html>

window.onhashchange=dothis当属性更改时;
函数changeTheHash()
{
var newHashValue=document.getElementById(“newHashInput”).value;
var currentLocation=window.location.pathname;
window.location.hash=newHashValue;
}
函数dothis whenthehashchanges()
{
警报(“哈希已更改!”);
}
散列更改测试
请参见:

浏览器是否支持window.onhashchange

请注意,在IE7兼容模式下运行的IE8报告窗口中的“onhashchange”为true, 即使不支持该事件,也要测试
document.documentMode

var docmode = document.documentMode;
if ('onhashchange' in window && (docmode === undefined || docmode > 7 )) {
    window.onhashchange = checkHash;
}