如何将函数绑定到DOM事件一次且仅一次,以便它们在触发事件时不会再次执行? [英] How to bind function to DOM events once and only once so that they will not execute for a second time in the triggering of event?

查看:104
本文介绍了如何将函数绑定到DOM事件一次且仅一次,以便它们在触发事件时不会再次执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有< a href ='[link'] rel ='Tab'> 形式的锚标签,我在页面加载时应用以下内容:('文件准备'我的意思是)

so I have anchor tags in the form of <a href='[link'] rel='Tab'> and I apply the following at page load:('document ready' I mean)

jQuery("a[rel*=Tab]").click(function(e) {
                e.preventDefault();//then I do some stuff to open it in jq ui tab}

现在的问题是,当我这样做,之后通过javascript生成新的链接,在我的情况下加载第二页用JqGrid有新的< a rel ='当我第一次运行 jQuery(a [rel * = Tab])时,neoTab'> 不存在。单击(function(e)所以它们不会工作...所以我可以运行 jQuery(a [rel * = Tab])。点击(函数(e)每一个创建新链接的事件,但旧链接会加载多个标签,所以我可以选择所有 a [rel * = Tab] 以前没选过?

now the problem is that when I do this and later through javascript new links are generated, in my case with loading a second page with JqGrid there are new <a rel='neoTab'>'s that did not exist when I first run jQuery("a[rel*=Tab]").click(function(e) so they won't work... so I can run jQuery("a[rel*=Tab]").click(function(e) at every event that creates new links but then the old links would load multiple tabs so is there a way I could select all the "a[rel*=Tab]"s that were not not selected before?

注意:我可以并且已经解决了通过下面的详细信息,通过算法的方法,我只是认为有一些我不知道的语法,以防止使用这个hack!

note: I can and have already solved this through an algorithmic approach as you can see through the details below, I just think there is some syntaxes I am not aware of to prevent the need to use this hack!

不必要的详细信息:

var neoTabitStat = 0;
var openTabs = new Array();
openTabs[0] = 'inbox';
if(!(currentBox=='inbox'||currentBox=='outbox') ) {var currentBox = 'inbox';}

function initNeoTab(a){
    if(neoTabitStat==0) {
        jQuery("a[rel*="+a+"]").click(function(e) {
            e.preventDefault();
            tabIt(jQuery(this).attr('href')+'&nohead=1',jQuery(this).attr('title'),jQuery(this).attr('data-hovercard'));
        });
    neoTabitStat++;
    }
}

function tabIt(a,b,c) {
    c = typeof(c) != 'undefined' ? c : 0;
    lastOpen = openTabs.length;
    if(lastOpen<6) { 
        if(openTabs.indexOf(c)<0) {
            openTabs[lastOpen] = c;
            jQuery("#tabs").tabs("add" , a , b, lastOpen+1 );
        }
        $tabs.tabs('select', openTabs.indexOf(c) );
    }else{
        var tabErrDig = jQuery('<div style="display:hidden">You have opened the maximum amount of tabs please close one to open another tab.</div>').appendTo('body');
        tabErrDig.dialog({width:500,title:'Maximum Tabs Opened',modal: true,
                buttons: {
                    Ok: function() {
                        jQuery( this ).dialog( "close" );
                    }
                }
        });
    }
}

jQuery(document).ready(function() {  initNeotab('nameOfrelAttr4existingLinks'); }
myGrid = jQuery("#list").jqGrid({/*alot of stuff...*/} , gridComplete: function() { initNeotab('nameOfrelAttr4generatedLinks');
}

UPDATE:
因为Wrikken建议jQuery()。直播我检查了它,这正是我需要但我不能让它真正起作用,在某些情况下不会起作用。对于其他编写不同解决方案的人,你是否熟悉.live()如果是这样的话,为什么我不能在这里使用它?

UPDATE: as Wrikken suggested jQuery().live I checked it out and it is exactly what I need but I can't get it to actually work, it won't work in some cases. For everyone else who wrote different solutions, are you fimiliar with .live() if so why is it that I can't use it here?

结论
特别感谢fudgey,patrick dw和wrikken。所有答案都很有帮助。。委托()。为我工作但由于某种原因 .live()没有。关于 jqGrid a的方式dd的元素到表。我要暂时保持这个问题。再次感谢您的帮助。全部+1

CONCLUSION special thanks to fudgey, patrick dw, and wrikken. All answers were helpful. .delegate(). worked for me but for some reason .live() didn't. something about the way jqGrid add's elements to the table. I'm gonna keep the question open for a while. Thanks alot again for all your help. +1 to all

推荐答案

我打算建议使用 .one() 但您似乎是在动态添加链接。所以我可能最好为已经点击的链接添加一个标志。正如帕特里克所建议的那样,委托最适合这一点,进行以下修改:

I was going to suggest using .one() but it appears you are adding links dynamically. So I might be best to add a flag to a link that has already been clicked. As Patrick suggests, delegate would be best for that, with these modifications:

jQuery("#list").delegate( 'a[rel*=Tab]', 'click', function() {

 if ($(this).is('.alreadyClicked')) { return false; }
 $(this).addClass('alreadyClicked');

 // do something 

});

这篇关于如何将函数绑定到DOM事件一次且仅一次,以便它们在触发事件时不会再次执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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