简体   繁体   English

我可以通过触发一些东西在jQuery中调用这个匿名函数吗?

[英]Can I call this anonymous function in jQuery by triggering something?

I'm using the hoverIntent jQuery plugin in place of jQuery's hover() method. 我使用的是hoverIntent jQuery插件来代替jQuery的hover()方法。 I want the mouseout event to be called automatically. 我希望mouseout事件被自动调用。

When using hover() , I can trigger the mouseout event by calling mouseout() . 使用hover() ,我可以通过调用mouseout()来触发mouseout事件。 This doesn't work when using hoverIntent. 使用hoverIntent时,这不起作用。

I also tried calling a named anonymous function, but it didn't work for me (and I hear older IEs don't like named anonymous functions). 我也尝试调用命名匿名函数,但是它对我不起作用(我听说较早的IE不喜欢命名匿名函数)。

Here is an example on jsFiddle. 这是jsFiddle上的示例。

If I auto invoke the mouseout function, it can't be called via hoverIntent. 如果我自动调用mouseout函数,则无法通过hoverIntent调用它。

Now I know I could do... 现在我知道我可以做...

$('something').hoverIntent(function() { }, something);

something();

But I was wondering if what I wanted was possible? 但是我想知道我想要的东西是否可能?

Thanks 谢谢

Unfortunately there's not a way besides a named function, not with how the plugin is structured...it's not stored in any way that's accessible later, only to the plugin's closure. 不幸的是,除了命名函数之外没有办法,不是插件的结构方式......它不会以任何可以在以后访问的方式存储,只能存储在插件的闭包中。

It's set here: 在这里设置:

$.fn.hoverIntent = function(f, g) {
    // default configuration options
    var cfg = {
        sensitivity: 7,
        interval: 100,
        timeout: 0
    };
    // override configuration options with user supplied object
    cfg = $.extend(cfg, g ? {
        over: f,
        out: g
    } : f);

...then after that all references are to that cfg object which is only accessible inside the plugin. ...之后,所有引用都是指那个只能在插件中访问的cfg对象。 If you wanted to change the plugin and store that cfg object via .data() for example you could, but as-is there's no way to access or trigger either anonymous handler. 如果你想更改插件并通过.data()存储cfg对象,例如你可以,但是因为没有办法访问或触发匿名处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM