gpt4 book ai didi

jquery:取消绑定(bind)加载的函数

转载 作者:行者123 更新时间:2023-12-01 06:07:47 25 4
gpt4 key购买 nike

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

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

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

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

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

谢谢,刘

最佳答案

根据您的评论,我认为您根本不需要解除绑定(bind):

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

调整图像大小时背景会发生什么应该是绑定(bind)到 $(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.

关于jquery:取消绑定(bind)加载的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3702365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com