将 jQuery .click() 函数作为变量传递
我正在使用选项卡式界面,并设置以下 jQuery 函数来处理选项卡的单击事件。
$(document).ready(function () {
$('a#foo').click(function() {
//content, various calls
return false;
});
});
上面是我的一个选项卡的示例,其他选项卡也在同一个文档就绪块中。我需要做的是使当前选定的选项卡无法重新单击,并且在其他一些情况下,我可以根据需要手动禁用选项卡。我通过以下方式实现了这一点:
$('a#play').unbind('click');
这工作正常,并且它肯定会禁用选项卡,但问题是重新绑定曾经存在的单击操作。我通过绑定函数实现了这一点:
$('a#foo').bind('click', function() {
//the same content and calls as before
return false;
});
这也可以正常工作,但是当我向用户界面添加选项卡时,它变得非常混乱。直接的解决方案似乎是将函数创建为变量,然后将其传递到初始单击创建和绑定事件中。就像这样:
var Foo = new function() {
//same content and calls as before
return false;
}
$('a#foo').click(Foo());
$('a#foo').bind(Foo());
由于某种原因,这似乎导致了浏览器崩溃问题。在这种情况下,是否无法将函数作为 var 传递,或者我只是做错了?或者,是否有更好的方法来实现我正在寻找的结果?谢谢。
I'm working with a tabbed interface and have the following jQuery function set up to handle the click events of my tabs.
$(document).ready(function () {
$('a#foo').click(function() {
//content, various calls
return false;
});
});
The above is an example of one of my tabs, the others are also within the same document ready block. What I needed to do was make it so the currently selected tab could not be re-clicked and that in some other cases I could manually disable tabs if needed. I achieved this via the following:
$('a#play').unbind('click');
This works fine, and it certainly disables the tabs but the problem then becomes rebinding the click action that was once there. I achieved this via the bind function:
$('a#foo').bind('click', function() {
//the same content and calls as before
return false;
});
This also works fine, but it has become exceedingly cluttered as I have added tabs to my UI. The immediate solution appears to be to create the function as a variable and then pass it into the initial click creation and into the binding event. Like so:
var Foo = new function() {
//same content and calls as before
return false;
}
$('a#foo').click(Foo());
$('a#foo').bind(Foo());
This, for one reason or another, seems to be causing browser crashing issues. Is it not possible to pass a function as a var in this case or am I just doing it wrong? Alternatively, is there a better way to achieve the results I'm looking for? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Foo
为您提供了该函数,但在其后添加()
意味着您正在调用该函数而不是传递函数本身。由于您正在调用该函数,false
最终会传递给click
和bind
,显然没有执行任何操作。您的其他一些问题可能是由于您模拟切换到该选项卡两次(调用事件处理程序两次)而导致的。^^ 应该做你想做的事。
目前我们对您的设计真正了解的是您正在使用单击事件处理程序来调用来切换选项卡。这部分很棒,但我们需要更多信息才能为您提供真正想要的更深入的答案。如果您将代码发布到
Foo
中,我们应该能够提供更多帮助。 :D编辑:感谢 SLaks♦ 注意到我错过的函数声明中的
new
。我将在他的解释中添加更多细节:正如您所期望的,
function() {}
是一个匿名函数。 javascript 中的new
有点令人困惑。当您调用一个函数并在其前面添加new
时,您正在使用该函数来实例化该函数中定义的类的实例。在 JS 中,与大多数其他语言不同,类的整个定义都在一个构造函数中,您可以从中设置所有实例变量,如下所示:要创建“类”的实例方法,请使用原型属性。 但是我刚刚意识到我已经偏离主题了。详细了解此处和此处。 :D
The
Foo
gives you the function, but adding()
's after it means you are calling the function instead of passing the function itself. Since you're calling the function,false
ends up getting passed toclick
andbind
, obviously not doing anything. Some of your other problems might result from the fact that you simulating switching to that tab twice (calling the event handler twice).^^ should do what you want.
Currently all we really know about your design is that you are calling using a click event handler to switch tabs. That part is awesome, but we'll need more info to give you the deeper answer you really want. If you post the code inside
Foo
we should be able to help a bit more. :DEDIT: credit to SLaks♦ for noticing the
new
in the function declaration that I missed. I'll add a little more detail to his explanation:The
function() {}
is an anonymous function as you would expect.new
in javascript is a little more confusing. When you call a function and precede it withnew
, you are using that function to instantiate an instance of a class defined in the function. In JS, unlike most other languages, the entire definition of a class is in one constructor function, from which you set all the instance variables, like so:To make instance methods of the 'class', you use the prototype attribute. However I just realized I've gotten super off-topic. Read more on that here and here. :D
您需要从函数定义中删除
new
,并在使用该函数时停止调用该函数。当您编写
var foo = new function(...) { ... }
时,您正在创建一个函数文字,然后将其作为构造函数调用。这相当于
没有
SomeClass
虚拟变量。您只需将函数文字分配给变量即可。
当您编写
.click(foo())
时,您调用foo
,并将结果传递给click
.除非 foo 返回一个函数,否则这不是您想要做的。
您需要通过删除括号来传递 foo 本身。
You need to remove
new
from the function definition and stop calling the function when using it.When you write
var foo = new function(...) { ... }
, you're making a function literal, then calling it as a constructor.It's equivalent to
without the
SomeClass
dummy variable.You need to simply assign the function literal to the variable.
When you write
.click(foo())
, you're callingfoo
, and passing the result toclick
.Unless
foo
returns a function, that's not what you want to do.You need to pass
foo
itself by removing the parentheses.因此,首先,click 接受一个函数,但是您可以在不使用 () 的情况下进行调用,因为 click 会在准备就绪时运行该函数。通过添加 (),您可以直接调用它。
其次,bind 需要一个字符串(您要绑定到什么事件)和一个函数(如上所述)...
使用以下内容:
希望有帮助:)
So firstly, click accepts a function, but you call without the () as click runs the function when ready. By adding the () you call it straight up.
Secondly, bind takes a string (what event you are binding to) AND a function (as above)...
Use the following:
Hope that helps :)
尝试:
为了获得您正在寻找的结果的更好方法,您可以在每个选项卡上都有一个类,例如
.tab
。这样,您就可以执行以下操作:...,而不必使用大量 id 。
Try:
As for a better way to achieve the result you're looking for, you could have a class on every tab, such as
.tab
. That way, you can just do:... without having to fluff around with a lot of
id
s.采用不同的方法,不要
unbind()
。我假设选项卡都在一个公共容器中。如果是这样,只需使用
delegate()
(docs)< /i> 方法在容器上放置处理程序。下面是一个通用代码示例:
这只会触发对不具有
.selected
类的.tab
元素的点击。您需要针对您的特定代码进行修改。Take a different approach, and do not
unbind()
.I assume the tabs are all in a common container. If so, just use the
delegate()
(docs) method to place a handler on the container.Here's a generic code example:
This will only trigger clicks on
.tab
elements that do not have the.selected
class. You'll need to modify for your specific code.添加括号会调用该函数,但如果您想让它变得更酷,您可以这样做,以便 Foo 返回要绑定的函数。
这利用了 javascript 的函数编程方面的一个,即闭包,这很酷,但不如其他一些答案那么有效。您应该对闭包进行一些研究,因为它们可以用来制作一些很酷的东西。
http://www.javascriptkit.com/javatutors/closures.shtml
Adding the parenthesis calls the function, but if you wanted to make it cool and stuff, you could make it so that Foo returned the function to be bound.
This makes use of one on javascript's function programming aspects, closures, which is cool, but not as efficient as some of the other answers. You should do some research about closures, as they can be used to make some cool stuff.
http://www.javascriptkit.com/javatutors/closures.shtml