Javascript Jquery禁用了一个事件,并在以后重新激活它

Javascript Jquery禁用了一个事件,并在以后重新激活它,javascript,javascript-events,Javascript,Javascript Events,1-我有一个带有数据记录属性的html标记 2-我想禁用它上的所有单击事件 3-当用户单击我的元素时,我想显示身份验证popin 4-当用户将被记录时,我希望启动比以前禁用的事件 我尝试了下面的代码,但没有找到“…”部分 //1-标记上已插入一些单击事件。 jQuery(“[data btnplay]”)。在('click',function()上{ 警惕(“玩”); 返回false; }); //2-禁用所有单击事件 jQuery(“[data needlogged]”)。关闭('click

1-我有一个带有数据记录属性的html标记

2-我想禁用它上的所有单击事件

3-当用户单击我的元素时,我想显示身份验证popin

4-当用户将被记录时,我希望启动比以前禁用的事件

我尝试了下面的代码,但没有找到“…”部分


//1-标记上已插入一些单击事件。
jQuery(“[data btnplay]”)。在('click',function()上{
警惕(“玩”);
返回false;
});
//2-禁用所有单击事件
jQuery(“[data needlogged]”)。关闭('click');
//3-添加单击事件以显示标识popin
var previouselement clicked=false;
jQuery('body')。在('click.needlogged','data needlogged]=“true”,function()上{
previousElementClicked=jQuery(此);
警报(“显示识别密码”);
返回false;
});
jQuery(document).on('loginsAccess',function()){
//4-在登录成功时,我需要删除“显示标识popin”事件。因此,将数据needlogged设置为false
jQuery(“[data needlogged]”)
.data('needlogged','false')
.attr('data-needlogged','false');
//4-启用比我们之前禁用的初始点击事件(参见第2点),然后执行。
// ...?
jQuery(“[data needlogged]”)。在('click');//它不工作
如果(上一个元素已单击){
PreviousElement已单击。获取(0)。单击();
}
});
谢谢你的帮助

谢谢你的回答

它不能解决我的问题

我会尽量解释得更好

当我在needlogged元素上声明click事件时,我不知道是否已经有其他click事件。因此,在您的示例中,如何替换警报(“播放”);由最初的事件

我需要找到一种方法

1-禁用元素上的所有单击事件

2-在同一元素上添加单击事件

3-当触发器启动时,执行我之前禁用的事件

所以,我找到了解决这个问题的办法

在我的例子中,我不需要禁用和启用某些事件,但我需要在另一个事件之前设置一个单击事件

<a href="/play" data-btnplay data-needlogged="true">Play</a>

<script>
// 1 - some click events has been plug on the tag.
jQuery('[data-btnplay]').on('click', function() {
    alert('play');
    return false;
});

// [name] is the name of the event "click", "mouseover", .. 
// same as you'd pass it to bind()
// [fn] is the handler function
jQuery.fn.bindFirst = function(name, fn) {
    // bind as you normally would
    // don't want to miss out on any jQuery magic
    this.on(name, fn);

    // Thanks to a comment by @Martin, adding support for
    // namespaced events too.
    this.each(function() {
        var handlers = $._data(this, 'events')[name.split('.')[0]];
        // take out the handler we just inserted from the end
        var handler = handlers.pop();
        // move it at the beginning
        handlers.splice(0, 0, handler);
    });
};

var previousElementClicked = false;

// set the needlogged as first click event
jQuery('[data-needlogged]').bindFirst('click', function(event) {
    //if the user is logged, execute the other click event
    if (userIsConnected()) {
        return true;
    }

    //save the click element into a variable to execute it after login success
    previousElementClicked = jQuery(this);

    //show sreenset
    jQuery(document).trigger('show-identification-popin');

    //stop all other event
    event.stopImmediatePropagation();
    return false;
});

jQuery(document).on('loginSuccess', function() {
    if (userIsConnected() && lastClickedElement && lastClickedElement.get(0)) {
        // if the user has connected with success, execute the click on the element who has been save before
        lastClickedElement.get(0).click();
    }
});

//1-标记上已插入一些单击事件。
jQuery(“[data btnplay]”)。在('click',function()上{
警惕(“玩”);
返回false;
});
//[name]是事件“单击”、“鼠标悬停”的名称。。
//与将其传递给bind()相同
//[fn]是处理函数
jQuery.fn.bindFirst=函数(名称,fn){
//像平常一样绑定
//我不想错过任何jQuery的魔力
本.on(姓名,fn);
//感谢@Martin的评论,添加了对
//名称空间的事件。
这个。每个(函数(){
变量处理程序=$。_数据(此“事件”)[name.split('..')[0]];
//从末端取出我们刚刚插入的处理程序
var handler=handlers.pop();
//在开头移动它
拼接(0,0,handler);
});
};
var previouselement clicked=false;
//将needlogged设置为首次单击事件
jQuery(“[data needlogged]”)。bindFirst('click',函数(事件){
//如果用户已登录,则执行另一个单击事件
如果(userIsConnected()){
返回true;
}
//将click元素保存到变量中,以便在登录成功后执行它
previousElementClicked=jQuery(此);
//显示sreenset
jQuery(document).trigger('show-identification-popin');
//停止所有其他活动
事件。stopImmediatePropagation();
返回false;
});
jQuery(document).on('loginsAccess',function()){
if(userIsConnected()&&lastClickedElement&&lastClickedElement.get(0)){
//如果用户已成功连接,请单击之前保存的元素
lastClickedElement.get(0.click();
}
});
可能是
<a href="/play" data-btnplay data-needlogged="true">Play</a>

<script>
// 1 - some click events has been plug on the tag.
jQuery('[data-btnplay]').on('click', function() {
    alert('play');
    return false;
});

// [name] is the name of the event "click", "mouseover", .. 
// same as you'd pass it to bind()
// [fn] is the handler function
jQuery.fn.bindFirst = function(name, fn) {
    // bind as you normally would
    // don't want to miss out on any jQuery magic
    this.on(name, fn);

    // Thanks to a comment by @Martin, adding support for
    // namespaced events too.
    this.each(function() {
        var handlers = $._data(this, 'events')[name.split('.')[0]];
        // take out the handler we just inserted from the end
        var handler = handlers.pop();
        // move it at the beginning
        handlers.splice(0, 0, handler);
    });
};

var previousElementClicked = false;

// set the needlogged as first click event
jQuery('[data-needlogged]').bindFirst('click', function(event) {
    //if the user is logged, execute the other click event
    if (userIsConnected()) {
        return true;
    }

    //save the click element into a variable to execute it after login success
    previousElementClicked = jQuery(this);

    //show sreenset
    jQuery(document).trigger('show-identification-popin');

    //stop all other event
    event.stopImmediatePropagation();
    return false;
});

jQuery(document).on('loginSuccess', function() {
    if (userIsConnected() && lastClickedElement && lastClickedElement.get(0)) {
        // if the user has connected with success, execute the click on the element who has been save before
        lastClickedElement.get(0).click();
    }
});