• <i id='p9Sas'><tr id='p9Sas'><dt id='p9Sas'><q id='p9Sas'><span id='p9Sas'><b id='p9Sas'><form id='p9Sas'><ins id='p9Sas'></ins><ul id='p9Sas'></ul><sub id='p9Sas'></sub></form><legend id='p9Sas'></legend><bdo id='p9Sas'><pre id='p9Sas'><center id='p9Sas'></center></pre></bdo></b><th id='p9Sas'></th></span></q></dt></tr></i><div id='p9Sas'><tfoot id='p9Sas'></tfoot><dl id='p9Sas'><fieldset id='p9Sas'></fieldset></dl></div>
  • <tfoot id='p9Sas'></tfoot>

      <legend id='p9Sas'><style id='p9Sas'><dir id='p9Sas'><q id='p9Sas'></q></dir></style></legend>

          <bdo id='p9Sas'></bdo><ul id='p9Sas'></ul>

        <small id='p9Sas'></small><noframes id='p9Sas'>

        处理 js 中的 URL 锚点更改事件

        时间:2023-09-05

        1. <i id='SXODU'><tr id='SXODU'><dt id='SXODU'><q id='SXODU'><span id='SXODU'><b id='SXODU'><form id='SXODU'><ins id='SXODU'></ins><ul id='SXODU'></ul><sub id='SXODU'></sub></form><legend id='SXODU'></legend><bdo id='SXODU'><pre id='SXODU'><center id='SXODU'></center></pre></bdo></b><th id='SXODU'></th></span></q></dt></tr></i><div id='SXODU'><tfoot id='SXODU'></tfoot><dl id='SXODU'><fieldset id='SXODU'></fieldset></dl></div>

                <bdo id='SXODU'></bdo><ul id='SXODU'></ul>

                <small id='SXODU'></small><noframes id='SXODU'>

                    <tbody id='SXODU'></tbody>

                  <legend id='SXODU'><style id='SXODU'><dir id='SXODU'><q id='SXODU'></q></dir></style></legend>
                  <tfoot id='SXODU'></tfoot>
                  本文介绍了处理 js 中的 URL 锚点更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何编写将在 URL 锚点发生任何更改时执行的 JavaScript 回调代码?

                  How can I write the JavaScript callback code that will be executed on any changes in the URL anchor?

                  例如从 http://example.com#ahttp://example.com#b

                  推荐答案

                  Google 自定义搜索引擎使用计时器检查哈希值是否与之前的值相匹配,而单独域上的子 iframe 会更新父级的位置哈希以包含大小iframe 文档的正文.当计时器捕捉到变化时,父级可以调整 iframe 的大小以匹配正文的大小,以便不显示滚动条.

                  Google Custom Search Engines use a timer to check the hash against a previous value, whilst the child iframe on a seperate domain updates the parent's location hash to contain the size of the iframe document's body. When the timer catches the change, the parent can resize the iframe to match that of the body so that scrollbars aren't displayed.

                  类似下面的东西可以达到同样的效果:

                  Something like the following achieves the same:

                  var storedHash = window.location.hash;
                  window.setInterval(function () {
                      if (window.location.hash != storedHash) {
                          storedHash = window.location.hash;
                          hashChanged(storedHash);
                      }
                  }, 100); // Google uses 100ms intervals I think, might be lower
                  

                  谷歌浏览器 5、Safari 5、Opera 10.60、Firefox 3.6 和 Internet Explorer 8 all 支持 hashchange 事件:

                  Google Chrome 5, Safari 5, Opera 10.60, Firefox 3.6 and Internet Explorer 8 all support the hashchange event:

                  if ("onhashchange" in window) // does the browser support the hashchange event?
                      window.onhashchange = function () {
                          hashChanged(window.location.hash);
                      }
                  

                  并把它放在一起:

                  if ("onhashchange" in window) { // event supported?
                      window.onhashchange = function () {
                          hashChanged(window.location.hash);
                      }
                  }
                  else { // event not supported:
                      var storedHash = window.location.hash;
                      window.setInterval(function () {
                          if (window.location.hash != storedHash) {
                              storedHash = window.location.hash;
                              hashChanged(storedHash);
                          }
                      }, 100);
                  }
                  

                  jQuery 还有一个插件可以检查 hashchange 事件并在必要时提供自己的 - http://benalman.com/projects/jquery-hashchange-plugin/.

                  jQuery also has a plugin that will check for the hashchange event and provide its own if necessary - http://benalman.com/projects/jquery-hashchange-plugin/.

                  编辑:更新浏览器支持(再次).

                  EDIT: Updated browser support (again).

                  这篇关于处理 js 中的 URL 锚点更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:检测到双击事件时需要取消单击/鼠标事件 下一篇:在 jQuery 中如何处理 oncut、oncopy 和 onpaste?

                  相关文章

                  最新文章

                  • <bdo id='RJJgZ'></bdo><ul id='RJJgZ'></ul>
                    <i id='RJJgZ'><tr id='RJJgZ'><dt id='RJJgZ'><q id='RJJgZ'><span id='RJJgZ'><b id='RJJgZ'><form id='RJJgZ'><ins id='RJJgZ'></ins><ul id='RJJgZ'></ul><sub id='RJJgZ'></sub></form><legend id='RJJgZ'></legend><bdo id='RJJgZ'><pre id='RJJgZ'><center id='RJJgZ'></center></pre></bdo></b><th id='RJJgZ'></th></span></q></dt></tr></i><div id='RJJgZ'><tfoot id='RJJgZ'></tfoot><dl id='RJJgZ'><fieldset id='RJJgZ'></fieldset></dl></div>
                  1. <tfoot id='RJJgZ'></tfoot>

                    <legend id='RJJgZ'><style id='RJJgZ'><dir id='RJJgZ'><q id='RJJgZ'></q></dir></style></legend>

                    1. <small id='RJJgZ'></small><noframes id='RJJgZ'>