Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.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
Javascript 如果您使用规范标记来防止这种情况发生,@Marco并不是说内容应该不同(这是重新加载的原因),在这种情况下,额外的索引实际上可能是有意义的。延迟重新加载的好处是什么onclick=“location.reload()”工作正常。谢谢。在我的例子中,它只适用_Javascript_Jquery_Html_Anchor - Fatal编程技术网

Javascript 如果您使用规范标记来防止这种情况发生,@Marco并不是说内容应该不同(这是重新加载的原因),在这种情况下,额外的索引实际上可能是有意义的。延迟重新加载的好处是什么onclick=“location.reload()”工作正常。谢谢。在我的例子中,它只适用

Javascript 如果您使用规范标记来防止这种情况发生,@Marco并不是说内容应该不同(这是重新加载的原因),在这种情况下,额外的索引实际上可能是有意义的。延迟重新加载的好处是什么onclick=“location.reload()”工作正常。谢谢。在我的例子中,它只适用,javascript,jquery,html,anchor,Javascript,Jquery,Html,Anchor,如果您使用规范标记来防止这种情况发生,@Marco并不是说内容应该不同(这是重新加载的原因),在这种情况下,额外的索引实际上可能是有意义的。延迟重新加载的好处是什么onclick=“location.reload()”工作正常。谢谢。在我的例子中,它只适用于setTimeout。我认为setTimeout是给浏览器一个机会来更改url(包括锚散列)。没有它,页面将重新加载,但没有新的位置/url。您也可以在单击链接后重新运行读取url的脚本,而无需重新加载页面。 <a href="?dum


如果您使用规范标记来防止这种情况发生,@Marco并不是说内容应该不同(这是重新加载的原因),在这种情况下,额外的索引实际上可能是有意义的。延迟重新加载的好处是什么
onclick=“location.reload()”
工作正常。谢谢。在我的例子中,它只适用于
setTimeout
。我认为
setTimeout
是给浏览器一个机会来更改url(包括锚散列)。没有它,页面将重新加载,但没有新的位置/url。您也可以在单击链接后重新运行读取url的脚本,而无需重新加载页面。
<a href="?dummy=$random#myanchor2"></a>
<a href="?dummy=myanchor2#myanchor2"></a>
<a href="example#myanchor1" class="myHash">example1</a>
<a href="example#myanchor2" class="myHash">example2</a>
<a href="example#myanchor3" class="myHash">example3</a>
<script type="text/javascript">
    $('a.myHash').click(function(e) {
        e.preventDefault(); // Prevent the browser from handling the link normally, this stops the page from jumping around. Remove this line if you do want it to jump to the anchor as normal.
        var linkHref = $(this).attr('href'); // Grab the URL from the link
        if (linkHref.indexOf("#") != -1) { // Check that there's a # character
            var hash = linkHref.substr(linkHref.indexOf("#") + 1); // Assign the hash to a variable (it will contain "myanchor1" etc
            myFunctionThatDoesStuffWithTheHash(hash); // Call whatever javascript you use when the page loads and pass the hash to it
            alert(hash); // Just for fun.
        }
    });
</script>
<a href="/example#myanchor2" 
    onclick="setTimeout(location.reload.bind(location), 1)">
</a>
<a href="/example?anc=myanchor2"></a>
window.onload = function() {
    var hash = document.location.hash.substring(1);
    if (hash.length == 0) {
        var anc = getURLParameter("anc");
        if (anc != null) {
            hash = document.location.hash = anc;
        }
    }
}
<a href="#hardcore" onclick="location.reload()">#hardcore</a>
<a href="#myanchor2" onclick="location.hash='myanchor2'; location.reload();">myanchor2</a>
function openHash(hash) {

  // hashy code goes here

  return false; // optional: prevents triggering href for onclick calls
}
// page load
$(function () {
  if(typeof location.hash != typeof undefined) {
    // here you can add additional code to trigger only on page load
    openHash(location.hash);
  }
});

// hash change
$(window).on('hashchange', function() {
  // here you can add additional code to trigger only on hash change
  openHash(location.hash);
});
<a href="#" onclick="openHash('super-custom-hash')">Magic</a>