JQuery 将其传递给函数

发布于 2025-01-04 18:06:12 字数 212 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扎心 2025-01-11 18:06:12

查看函数名称 capIncapOut 可以看出它们是两种不同的行为。我相信您对 mouseentermouseleave 事件有两种不同的行为。 hover 方法可以采用 2 种方法,一种用于 mouseenter,另一种用于 mouseleave。您可以尝试这个

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

您可以在 capIncapOut 中使用 this 它将指向您悬停的 .slide 元素在。

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

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

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();
}
殊姿 2025-01-11 18:06:12
$(".slide").hover(function(){
    capIn.apply(this);
    capOut.apply(this);
});

请参阅此处:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function /apply

更新:

如果 capIn 用于 mouseenter 并且 capOut 用于mouseleave,那么:

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

ShankarSangoli的解决方案比较简洁,但是如果除了this之外还需要传递任何参数,那么:capIn.apply(this,arguments)。

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

情归归情 2025-01-11 18:06:12

这对我有用:

function capOut(jq) {
    alert(jq.hasClass('slide'))
}

$(".slide").hover(function () {
    capIn(this),
    capOut($(this)) //to pass a jQuery object wrapping 'this'
});

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'
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文