jQuery插件:处理事件 [英] jQuery Plugin: Handling Events

查看:122
本文介绍了jQuery插件:处理事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个jQuery插件,我想知道是否可以让插件听事件而不是事件触发。



所以而不是这样:

  $('#element_id')。mouseover(function(){
$这个).plugin();
});

$('#element_id')。mouseout(function(){
$(this).pluginHide();
});

我想尝试这样做:

  $('#element_id')。plugin(????,????) 

将这些引用替换为应该触发它的元素和事件的某种引用显示或隐藏我仍然用jQuery让我的脚湿润,所以如果一个解决方案是很明显的话,我可以忍受。

解决方案

之后尝试一下,看起来我正在寻找内置的bind()功能。解决我原来的问题:

  jQuery.fn.plugin = function(show,hide){
this.bind (show,function(){
//做某些事情显示
};

this.bind(hide,function(){
//做某事隐藏
};
};


I'm currently working on a jQuery plugin and I'm wondering if it would be possible to have the plugin listen for events rather than being triggered by events.

So instead of this:

$('#element_id').mouseover(function() {
    $(this).plugin();
});

$('#element_id').mouseout(function() {
    $(this).pluginHide();
});

I want to try to do something like this:

$('#element_id').plugin(????, ????);

And replace the ?'s with some sort of reference to the element and event that should trigger it to show or hide. I'm still getting my feet wet with jQuery, so bear with me if a solution to this is painfully obvious.

解决方案

After experimenting a bit, it looks like I was looking for the built-in "bind()" function. To solve my original problem:

jQuery.fn.plugin = function(show, hide) {
    this.bind(show, function() {
        // Do something to show.
    };

    this.bind(hide, function() {
        // Do something to hide.
    };
};

这篇关于jQuery插件:处理事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆