hey, Im trying to make a call a jquery function and pass some args with it in the form of
嘿,我试图调用一个jquery函数,并以它的形式传递一些args
$('#button').mouseenter(exampleFunction(arg1,arg2));
function exampleFunction(arg1,arg2)
The function works fine with no args written like this.
该函数工作正常,没有像这样编写的args。
$('#button').mouseenter(exampleFunction);
function exampleFunction;
but as soon as i add () to put args in the function stops working.
但是只要我添加()把args放在函数停止工作。
like this:
$('#button').mouseenter(exampleFunction());
It seems like this is some sort of jquery syntax error on my part
这似乎是我的某种jquery语法错误
here's the actual code
这是实际的代码
<script type="text/javascript">
$(document).ready(function() {
$('.section').mouseover(function(){
$('#nav2').fadeOut(0).animate({"height":"30px"}, 250);
});
$('#section1').hover(navSelect);
function navSelect(){
if ( $('.interior').is(':hidden')){
$('.subSection').fadeOut(250);
$('.interior').delay(250).fadeIn(250);
$('#selector').animate({"left":"0px"},250);
}}
$('#section2').mouseenter(function(){
if ( $('.exterior').is(':hidden')){
$('.subSection').fadeOut(250);
$('.exterior').delay(250).fadeIn(250);
$('#selector').animate({"left":"100px"},250);
}
});
$('#section3').mouseenter(function(){
if ( $('.view').is(':hidden')){
$('.subSection').fadeOut(250);
$('.view').delay(250).fadeIn(250);
$('#selector').animate({"left":"200px"},250);
}
});
});
</script>
4 个解决方案
#1
0
the function you bind to on an event in jQuery is passed the 'event' object from jQuery's invocation of the method.
在jQuery中绑定到事件的函数是从jQuery调用方法传递的'event'对象。
jQuery("#button").mouseenter(function(evt){[ do something ]});
jQuery(“#button”)。mouseenter(function(evt){[do something]});
if you want to do something like
如果你想做点什么
$('#button').mouseenter(exampleFunction(arg1,arg2));
in order to templatize a function that you want to have bound to the event, you could construct a function like so:
为了模板化你想要绑定到事件的函数,你可以构造一个这样的函数:
function exampleFunction(arg1, arg2){
return function(evt){
jQuery(this).animate({width: arg1, height: arg2},250)
};
}};
function exampleFunction(arg1,arg2){return function(evt){jQuery(this).animate({width:arg1,height:arg2},250)}; }};
having it return the function that is bound (and thus, passed the event object), and then bind it to elements like
让它返回绑定的函数(从而传递事件对象),然后将它绑定到像这样的元素
jQuery("#button").mouseenter(exampleFunction("50%","50%"))
.mouseleave(exampleFunction("100%","100%");
jQuery(“#button”)。mouseenter(exampleFunction(“50%”,“50%”))。_mouseleave(exampleFunction(“100%”,“100%”);
#2
5
Functions in JavaScript are first-class objects. You can put them in variables, return them from other functions, etc. When you write $('#button').mouseenter(exampleFunction(arg1,arg2));
, you are saying "Run exampleFunction with these arguments, and use its return value as a callback".
JavaScript中的函数是一流的对象。您可以将它们放在变量中,从其他函数返回它们等。当您编写$('#button')。mouseenter(exampleFunction(arg1,arg2));时,您说“使用这些参数运行exampleFunction,并使用它返回值作为回调“。
To get jQuery to call the function with those arguments later, you can use an anonymous inline function:
要让jQuery稍后使用这些参数调用该函数,您可以使用匿名内联函数:
$('#button').mouseenter(function() {
exampleFunction(arg1,arg2)
});
Your no-argument function will get called, and pass the right arguments to the function you actually want to call. It's sort of a poor-man's closure.
您的无参数函数将被调用,并将正确的参数传递给您实际要调用的函数。这是一个穷人的关闭。
#3
1
the difference between funktion and funktion() is one is a pointer to a function and the other is the result of function execution.
funktion和funktion()之间的区别在于一个是指向函数的指针,另一个是函数执行的结果。
try
mouseenter(function(a,b){....})
there you are defining the function and passing it in. The function defined takes 2 args, a and b.
在那里你定义了函数并将其传入。定义的函数需要2个args,a和b。
#4
0
I believe there's a jQuery plugin for that.
Look here: http://plugins.jquery.com/
我相信有一个jQuery插件。看这里:http://plugins.jquery.com/
#1
0
the function you bind to on an event in jQuery is passed the 'event' object from jQuery's invocation of the method.
在jQuery中绑定到事件的函数是从jQuery调用方法传递的'event'对象。
jQuery("#button").mouseenter(function(evt){[ do something ]});
jQuery(“#button”)。mouseenter(function(evt){[do something]});
if you want to do something like
如果你想做点什么
$('#button').mouseenter(exampleFunction(arg1,arg2));
in order to templatize a function that you want to have bound to the event, you could construct a function like so:
为了模板化你想要绑定到事件的函数,你可以构造一个这样的函数:
function exampleFunction(arg1, arg2){
return function(evt){
jQuery(this).animate({width: arg1, height: arg2},250)
};
}};
function exampleFunction(arg1,arg2){return function(evt){jQuery(this).animate({width:arg1,height:arg2},250)}; }};
having it return the function that is bound (and thus, passed the event object), and then bind it to elements like
让它返回绑定的函数(从而传递事件对象),然后将它绑定到像这样的元素
jQuery("#button").mouseenter(exampleFunction("50%","50%"))
.mouseleave(exampleFunction("100%","100%");
jQuery(“#button”)。mouseenter(exampleFunction(“50%”,“50%”))。_mouseleave(exampleFunction(“100%”,“100%”);
#2
5
Functions in JavaScript are first-class objects. You can put them in variables, return them from other functions, etc. When you write $('#button').mouseenter(exampleFunction(arg1,arg2));
, you are saying "Run exampleFunction with these arguments, and use its return value as a callback".
JavaScript中的函数是一流的对象。您可以将它们放在变量中,从其他函数返回它们等。当您编写$('#button')。mouseenter(exampleFunction(arg1,arg2));时,您说“使用这些参数运行exampleFunction,并使用它返回值作为回调“。
To get jQuery to call the function with those arguments later, you can use an anonymous inline function:
要让jQuery稍后使用这些参数调用该函数,您可以使用匿名内联函数:
$('#button').mouseenter(function() {
exampleFunction(arg1,arg2)
});
Your no-argument function will get called, and pass the right arguments to the function you actually want to call. It's sort of a poor-man's closure.
您的无参数函数将被调用,并将正确的参数传递给您实际要调用的函数。这是一个穷人的关闭。
#3
1
the difference between funktion and funktion() is one is a pointer to a function and the other is the result of function execution.
funktion和funktion()之间的区别在于一个是指向函数的指针,另一个是函数执行的结果。
try
mouseenter(function(a,b){....})
there you are defining the function and passing it in. The function defined takes 2 args, a and b.
在那里你定义了函数并将其传入。定义的函数需要2个args,a和b。
#4
0
I believe there's a jQuery plugin for that.
Look here: http://plugins.jquery.com/
我相信有一个jQuery插件。看这里:http://plugins.jquery.com/