Jquery 在 hashchange 上调用函数

发布于 2024-11-17 17:24:05 字数 635 浏览 4 评论 0 原文

我有 2 个问题

  1. 我正在使用 hashchange 插件...所以我想知道每次发生 hashchange 时都会调用下面的函数...因为我的代码和代码函数中有类似的东西显然似乎没有被称为

    $(文档).ready(function()
    {
        // 这里的函数
    });
    
  2. 另一方面,如果我删除 hashchange,就像如果我做 http://abc.com/a.htm#http://abc.com/b.htm

问题是我的页面结构有点不同......这是页面结构的小提琴,它在更高的层次上解释了我想要实现的目标 jsfiddle.net/vBKWd/9 ...在哈希更改时,我第 1 页上的 div c 被第 2 页替换,反之亦然 ....和我展示的 js 函数下面只被调用一次,而不是在 hashchange 之后

调用或者有什么方法可以将函数与 div 绑定,这样每当 div 被替换时,函数就会被调用?

I have 2 questions

  1. I am using the hashchange plugin .... so I want to know would a function as below, be called everytime a hashchange occurs... because I have something like that in my code and the code function apparently doesnt seems to be called

    $(document).ready(function()
    {
        // function here
    });
    
  2. On the other have if I remove the hashchange as in If i make http://abc.com/a.htm#http://abc.com/b.htm as http://abc.com/b.htm
    the code works fine

the problem is the structure of my pages is a bit different .... here is the fiddle with the page structure that explains on a higher level what I am trying to achieve jsfiddle.net/vBKWd/9 ... on hash change jus the div c on my page 1 gets replaced by page 2 and vice versa .... and the js function that I have shown below is getting called only once and not after hashchange

Or is therre any way I can bind the function with the div so that whenever the div is replace the function get called?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷荒 2024-11-24 17:24:05

不,就绪处理程序仅在文档就绪时调用,而不是在哈希更改时调用。您应该使用 hashchange 事件来代替:

$(window).hashchange(function () {
    // function here
});

示例:http://jsfiddle。净/vBKWd/2/

No, a ready handler is only called on document ready, not on hash change. You should use the hashchange event for that, instead:

$(window).hashchange(function () {
    // function here
});

Sample: http://jsfiddle.net/vBKWd/2/

别挽留 2024-11-24 17:24:05

在下面的文档中准备好编写代码

$(window).bind('hashchange', function () {
                 //code here
});

In document ready wirte code below

$(window).bind('hashchange', function () {
                 //code here
});
极度宠爱 2024-11-24 17:24:05

在这种情况下使用 live

$(document).ready(function()
{   
   $(selector).live(hashchange, function(){
    // your code goes here

    });
});

use live in this case

$(document).ready(function()
{   
   $(selector).live(hashchange, function(){
    // your code goes here

    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文