Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jqueryon方法了解url中的哈希值何时更改_Jquery - Fatal编程技术网

使用jqueryon方法了解url中的哈希值何时更改

使用jqueryon方法了解url中的哈希值何时更改,jquery,Jquery,所以我有一个url,可以是mywebsite.com/index.html#Jobs 我首先需要在散列之后获得名称,我通过 var $ext = $(location).attr('hash'); $ext= $ext.replace("#",""); 问题是知道何时在实时事件而不是页面刷新上更改了此哈希标记 这就是我尝试过的 var $ext = $(location).attr('hash'); $ext.('change', function() { console.log("c

所以我有一个url,可以是mywebsite.com/index.html#Jobs

我首先需要在散列之后获得名称,我通过

var $ext = $(location).attr('hash');
$ext= $ext.replace("#","");
问题是知道何时在实时事件而不是页面刷新上更改了此哈希标记

这就是我尝试过的

var $ext = $(location).attr('hash');

$ext.('change', function() {

  console.log("changed");
});
我知道这不起作用,因为$ext不是一个元素,但我希望得到一些帮助

欢迎任何想法。

使用此插件:

并使用此代码:

$(window).hashchange( function() {
    var hash = location.hash;
    myfunc(hash);
});
function myfunc(hash)  {
    alert(hash);
}
您也可以使用,但有一些兼容性问题:

window.onhashchange = function() {
    alert(window.location.hash);
}
以及检查浏览器是否支持hashchange:

if ("onhashchange" in window) {
    alert("The browser supports the hashchange event!");
}