最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery pass this to function - Stack Overflow

programmeradmin2浏览0评论

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.

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

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.

$(".slide").hover(function(){capIn();capOut();});
Share Improve this question asked Feb 10, 2012 at 15:21 Jonathan EckmanJonathan Eckman 2,0774 gold badges25 silver badges51 bronze badges 1
  • 1 Is that your actual code? Are you doing nothing but calling those functions? Do you really want them both called for the mouseenter and mouseleave events? – user1106925 Commented Feb 10, 2012 at 15:25
Add a comment  | 

3 Answers 3

Reset to default 11

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

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

You can use this inside capIn and capOut it will point to .slide element which you hovered on.

function capIn(){
   var $childrens = $(this).children();
}

function capOut(){
   var $childrens = $(this).children();
}
$(".slide").hover(function(){
    capIn.apply(this);
    capOut.apply(this);
});

See here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply

UPDATE:

If capIn is for mouseenter and capOut is for mouseleave, then:

$(".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.

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'
});
发布评论

评论列表(0)

  1. 暂无评论