javascript JQuery 将此传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9230057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery pass this to function
提问by Jonathan Eckman
I need to send this
to the capIn() and capOut() functions so that I can target the children divs of the correct .slide. How do I pass this
to a function. this
would be what is hovered.
我需要发送this
到 capIn() 和 capOut() 函数,以便我可以定位正确 .slide 的子 div。我如何传递this
给一个函数。this
将是什么悬停。
$(".slide").hover(function(){capIn();capOut();});
回答by ShankarSangoli
Looking at the function names capIn
and capOut
it makes sense that they are 2 different behaviors. I believe you have 2 different behaviors on mouseenter
and mouseleave
events. hover
method can take 2 methods, one for mouseenter
and another for mouseleave
. You can try this
纵观函数名capIn
和capOut
它是有道理的,他们是2点不同的行为。我相信您对mouseenter
和mouseleave
事件有两种不同的行为。hover
方法可以采用 2 种方法,一种是 for mouseenter
,另一种是 for mouseleave
。你可以试试这个
$(".slide").hover(capIn, capOut);
You can use this
inside capIn
and capOut
it will point to .slide
element which you hovered on.
您可以使用this
inside capIn
,capOut
它将指向.slide
您悬停在其上的元素。
function capIn(){
var $childrens = $(this).children();
}
function capOut(){
var $childrens = $(this).children();
}
回答by dgilland
$(".slide").hover(function(){
capIn.apply(this);
capOut.apply(this);
});
See here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply
请参阅此处:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply
UPDATE:
更新:
If capIn
is for mouseenter
and capOut
is for mouseleave
, then:
如果capIn
是mouseenter
并且capOut
是mouseleave
,那么:
$(".slide").hover(function(){
capIn.apply(this);
}, function(){
capOut.apply(this);
});
ShankarSangoli
's solution is more succinct, but if any arguments need to be passed in addition to this
, then: capIn.apply(this, arguments)
can be used.
ShankarSangoli
的解决方案更简洁,但如果除 之外还需要传递任何参数this
,则capIn.apply(this, arguments)
可以使用:。
回答by pete
This works for me:
这对我有用:
function capOut(jq) {
alert(jq.hasClass('slide'))
}
$(".slide").hover(function () {
capIn(this),
capOut($(this)) //to pass a jQuery object wrapping 'this'
});