2010-09-13 66 views
2

我在加载页面时将调整图像背景的功能,jQuery的:取消绑定加载功能

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

,我想“卸载”或“删除”这加载功能,当我点击一个按钮,这样我就可以重新加载,而不是在加载页面的顶部的这个功能,

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

,但我不能使它工作!有什么想法吗?

感谢, 刘

+0

你这是什么意思,“加载此功能重新”从那里如何 – Pointy 2010-09-13 16:33:27

+0

这是很难猜测正确的'.unbind(“调整”)'行不能够看到加载???你的'.bind()'或'.resize()'行。:-) – 2010-09-13 17:08:05

回答

0

根据您的意见,我不认为你需要在所有的解除绑定:

您首先要加载的背景时,页面加载,然后你想点击发生时加载背景。

发生时,调整图像的大小应该是绑定到$(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. 
+0

谢谢。只是尝试过,但它仍然工作...图像不会再调整这个$(窗口).resize(loadBackground); loadBackground函数必须位于$(document).ready(function(){loadBackground();}); – laukok 2010-09-13 16:58:32

+1

@lauthiamkok - 这是因为你必须创建一个对你的函数的引用,像这样:'var loadBackground = function(){...}'...看看代码在[jsFiddle](http ://jsfiddle.net/xTfCX/) – 2010-09-13 17:01:36

+0

@lauthimakok - 我的答案中的所有代码都在'$(document).ready(function(){...})里面;' – 2010-09-13 17:09:52