如何多次调用函数? [英] How can I call a function multiple times?

查看:418
本文介绍了如何多次调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:我正在开发一个包含2个样式表的移动友好型网站,一个用于面向PC的可视化,另一个用于移动可视化。与2 CSS一起,当从一种可视化模式切换到另一种时,我需要一个函数来修改菜单上的一些 href 属性。

My issue is: I'm developing a mobile-friendly website with 2 stylesheets, one for the "PC-oriented" visualization and the other for the mobile visualization. Along with the 2 CSS, I need a function to modify some href attributes on the menu when switching from one visualization mode to the other.

我不会深入研究这些东西的细节,因为它会很长并且可能很无聊,无论如何我需要在两者 > $(document).ready AND $(window).resize ,即每次调整窗口大小。

I'm not going deep into the details of the stuff as it would be very long and probably boring, anyway I need the function to be called both on $(document).ready AND on $(window).resize, that is every time the window is resized.

所以我声明了一个函数,并将其命名为 goToAnchor ,以便能够多次调用它:

So I declared a function and named it goToAnchor in order to be able to call it more than once:

$(document).ready(function() {
   function goToAnchor() {  ... modify some href attr ... }
   goToAnchor();
});

这很好,但正如我所说,我还需要每次窗口都执行的功能调整大小,所以我认为代码可能如下所示:

That's perfectly fine, but as I said I also need the function to be executed every time the window is resized, so I thought the code might look like:

$(document).ready(function() {
   function goToAnchor() {  ... modify some href attr ... }
   goToAnchor();
   $(window).resize(goToAnchor());
});

问题很简单,该功能不能在 resize上运行。我检查了jQuery文档并注意到关于将函数链接到事件的每个示例都是这样写的:

The problem is simple, the function doesn't run on resize. I checked the jQuery documentation and noticed that every example regarding linking a function to an event is written like this:

$(window).resize(function() { ...do stuff...} );

我不会使用匿名函数,因为我之前已经编写了这个函数。我想做的事情非常简单:多次调用一个函数而不需要使用匿名函数来复制我的代码(毕竟,这就是函数应该如何工作,对吧?)。

I wouldn't resort using an anonymous function though, as I have already written the function previously. What I want to do is extremely simple: to call a function multiple times without resorting to anonymous functions to "duplicate" my code (after all, that's how functions are supposed to work, right?).

推荐答案

您想要替换: $(window).resize(goToAnchor()); with $(窗口).resize(goToAnchor);

You want to replace: $(window).resize(goToAnchor()); with $(window).resize(goToAnchor);.

您的代码正在传递 goToAnchor 函数和你想要做的是传递对函数的引用,我们通过省略括号来做。

Your code is passing the response of the goToAnchor function and what you want to do is pass a reference to the function, which we do by omitting the parenthesis.

但是你可以也这样做:

$(function() {
   $(window).bind('resize', function () { /*resize code*/ }).trigger('resize');
});

调整大小时,这会有效地运行代码事件在窗口对象以及 document.ready .trigger(')上触发resize')使 document.ready 上的 resize 事件触发。

This effectively runs the code when the resize event fires on the window object as well as on document.ready (.trigger('resize') makes the resize event fire on document.ready).

这篇关于如何多次调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆