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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:59:54  来源:igfitidea点击:

JQuery pass this to function

javascriptjqueryfunctionhoverthis

提问by Jonathan Eckman

I need to send thisto the capIn() and capOut() functions so that I can target the children divs of the correct .slide. How do I pass thisto a function. thiswould be what is hovered.

我需要发送this到 capIn() 和 capOut() 函数,以便我可以定位正确 .slide 的子 div。我如何传递this给一个函数。this将是什么悬停。

$(".slide").hover(function(){capIn();capOut();});

回答by ShankarSangoli

Looking at the function names capInand capOutit makes sense that they are 2 different behaviors. I believe you have 2 different behaviors on mouseenterand mouseleaveevents. hovermethod can take 2 methods, one for mouseenterand another for mouseleave. You can try this

纵观函数名capIncapOut它是有道理的,他们是2点不同的行为。我相信您对mouseentermouseleave事件有两种不同的行为。hover方法可以采用 2 种方法,一种是 for mouseenter,另一种是 for mouseleave。你可以试试这个

$(".slide").hover(capIn, capOut);

You can use thisinside capInand capOutit will point to .slideelement which you hovered on.

您可以使用thisinside capIncapOut它将指向.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 capInis for mouseenterand capOutis for mouseleave, then:

如果capInmouseenter并且capOutmouseleave,那么:

$(".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'
});