Jquery:trigger';单击';在';鼠标输出';动态地

Jquery:trigger';单击';在';鼠标输出';动态地,jquery,angularjs,Jquery,Angularjs,我知道这个问题似乎很容易,但事实并非如此 请允许我详细说明 任务 将动态提供的事件与动态提供的元素绑定,以引发动态提供的事件 我知道上面的任务看起来很混乱。因此,为了做到这一点,我写了一些代码,因为它似乎不工作。非常感谢您的任何建议 非常感谢 我有3个变量 var elementId = 't'; /// the id of an element on which the binding is performed var triggerOn = 'mouseout'; /// the even

我知道这个问题似乎很容易,但事实并非如此

请允许我详细说明

任务 将动态提供的事件与动态提供的元素绑定,以引发动态提供的事件

我知道上面的任务看起来很混乱。因此,为了做到这一点,我写了一些代码,因为它似乎不工作。非常感谢您的任何建议

非常感谢

我有3个变量

var elementId = 't'; /// the id of an element on which the binding is performed
var triggerOn = 'mouseout'; /// the event that is going to be bind
var triggerEvent = 'click'; /// the event that is going to be raised on the occurrence of the above event


///function that is binding the event with element
var triggeror = function (eleId, trgOn, TrgEve, isBind) {

    /// if isbind is true then bind else unbind
    if (isBind === true) {
        $('#' + eleId).bind(trgOn, triggerFun);  /// problem
    }
    else {
        $('#' + eleId).unbind(trgOn, triggerFun); /// problem 
    }

    var self = this;

    /// the event that is fired with the binded event is occured
    function triggerFun() {
        $('#' + self.eleId).trigger(self.TrgEve); ///trigger the another event
    }
};



/// fired when the document is ready
$(document).ready(function () {

    /// on text box click show an alert and un bind the trigeror
    $("#t").bind('click', function () {
        /// showing alert
        alert();
        /// unbinding
        triggeror(elementId, triggerOn, triggerEvent, false);
    });

    /// bind the mouseout to click on textbox
    triggeror(elementId, triggerOn, triggerEvent, true);
});
问题 事件未被引发

修复

有一些问题,例如:

第一次单击后解除绑定

绑,再绑,再绑

绑定时,调用函数而不是链接到函数

$('#' + eleId).bind(trgOn,triggerFun(eleId, TrgEve));
改为

$('#' + eleId).bind(trgOn,function(){ triggerFun(eleId, TrgEve) });

查看并让我知道,plunkr中的问题是绑定事件中的函数会立即被调用

尝试在如下函数中添加它们:-

var triggeror = function (eleId, trgOn, TrgEve, isBind) {

    /// if isbind is true then bind else unbind
    if (isBind === true) {
        $('#' + eleId).bind(trgOn, function(){
          triggerFun(eleId, TrgEve);
        });
    }
    else {
        $('#' + eleId).unbind(trgOn);
    }
};

注意:

.bind()

从jQuery1.7开始,.on()方法是 将事件处理程序附加到文档


对于任何打开Plunkr的人,请准备好大量警报。@SatejS我打开了,然后向下滚动..:(我认为没有必要向解除绑定功能添加处理程序先生,解除绑定仍然不起作用。每次调用mouseout时都会调用它。解除绑定不起作用。每次鼠标离开时都会记录控制台日志。我在单击事件中对其进行了评论,我认为这是一个错误,而不是“一次性操作”事件,取消对它的注释,您就完成了。我已经访问了您提供的链接及其提供的
triggerFun[“t”,“click”]index.js:35单击t index.js:24 triggerFun[“t”,“click”]index.js:35单击t index.js:24 triggerFun[“t”,“click”]index.js:35单击t index.js:24 triggerFun[“t”,“click”]index.js:35点击t
顺便问一下,你能澄清什么时候必须调用解除绑定吗?你告诉我,当点击被触发时,这是一个一次性操作=解除绑定,但在另一条评论中,你说这不是一次性操作。请澄清,我可以是一次性操作,也可以是1000次操作,具体取决于计数器作为参数传递。这只是一个虚拟操作实际上,如果计数器超过计数的限制,那么触发器应该被取消绑定。
var triggeror = function (eleId, trgOn, TrgEve, isBind) {

    /// if isbind is true then bind else unbind
    if (isBind === true) {
        $('#' + eleId).bind(trgOn, function(){
          triggerFun(eleId, TrgEve);
        });
    }
    else {
        $('#' + eleId).unbind(trgOn);
    }
};