jquery:取消绑定加载的函数

发布于 2024-09-19 11:12:06 字数 368 浏览 2 评论 0原文

我有一个函数,可以在加载页面时调整图像背景的大小,

$(document).ready(function(){
loadBackground();
});

并且我想在单击按钮时“卸载”或“删除”此加载的函数,以便我可以重新加载此函数,而不是将其加载到顶部页面的,

$('.get-image').click(function(){
        $(window).unbind("resize",loadBackground);
        loadBackground();
 return false;
 });

但我无法使它工作!有什么想法吗?

谢谢, 刘

I have a function which will resize the image background when the page is loaded,

$(document).ready(function(){
loadBackground();
});

and I want to 'unload' or 'remove' this loaded function when I click on a button so that I can load this function anew instead of loading it on top of the page,

$('.get-image').click(function(){
        $(window).unbind("resize",loadBackground);
        loadBackground();
 return false;
 });

but I can't make it work! any ideas please?

thanks,
Lau

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

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

发布评论

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

评论(1

攀登最高峰 2024-09-26 11:12:06

根据您的评论,我认为您根本不需要取消绑定:

您首先要在加载页面时加载背景,然后在单击发生时加载背景。

调整图像大小时背景会发生什么应该是绑定到 $(window).resize() 的另一个单独的函数...所以整个事情看起来像这样:(

$(function() { ... });$(document).ready(function() { ... }) 含义相同,只是写起来更快:

$(function() { 
      // Define load BG
    function loadBackground() {
        /// ...
    }

      // Define what happens to the BG image on resize
      // this doesn't have to change. It should refer
      // to the generic `background-image`
    $(window).resize(function() { 
        // ...
    });

      // load BG when page is ready:
    loadBackground();

      // load another BG if there's a click:
      // This will effectively cancel the previous load bg
    $('.get-image').click(function() { 
        loadBackground();
        return false;
    });    
});

// I'm assuming you're somehow changing which BG image is loaded
// something like loadBackground(image)... and you could initially 
// load a default image, and then, if there's a click, you could
// determine what image should be based on what was clicked.

Based on your comments, I don't think you need to unbind at all:

You first want to load the background when the page is loaded, and then you want to load the background when a click happens.

What happens to the background when the image is resized should be another separate function that is bound to $(window).resize()... so the whole thing will look like this:

( $(function() { ... }); means the same as $(document).ready(function() { ... }) it's just faster to write:

$(function() { 
      // Define load BG
    function loadBackground() {
        /// ...
    }

      // Define what happens to the BG image on resize
      // this doesn't have to change. It should refer
      // to the generic `background-image`
    $(window).resize(function() { 
        // ...
    });

      // load BG when page is ready:
    loadBackground();

      // load another BG if there's a click:
      // This will effectively cancel the previous load bg
    $('.get-image').click(function() { 
        loadBackground();
        return false;
    });    
});

// I'm assuming you're somehow changing which BG image is loaded
// something like loadBackground(image)... and you could initially 
// load a default image, and then, if there's a click, you could
// determine what image should be based on what was clicked.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文