代码之家  ›  专栏  ›  技术社区  ›  andres descalzo

jQuery事件。将信息发送到他被叫到的事件

  •  0
  • andres descalzo  · 技术社区  · 15 年前

    向Click事件发送消息以了解消息的调用位置的最佳方法是什么?.

    $("#mybutton").click(function(ev){
    
       if (called from funcion One or Two){
         //my code
       }
    
    });
    
    funOne = function(){
      $("#mybutton").click();   
    };
    
    funTwo = function(){
      $("#mybutton").click();   
    };
    

    编辑:

    在A上 "trigger" 我有一个小的解决方案,但取决于所有实现参数“data”

    编辑(二):

    我的解决方案基于“@roatin marth”的答案。

    jQuery.fn.trigger = function(event, data) {
    
        var type = event.type || event,
            expando = "jQuery" + (+new Date);
    
        event = typeof event === "object" ?
        // jQuery.Event object
            event[expando] ? event :
        // Object literal
            jQuery.extend(jQuery.Event(type), event) :
        // Just the event type (string)
            jQuery.Event(type);
    
        if (!event.caller) {
            var xcaller = "";
            try {
                xcaller = arguments.callee.caller;
            } catch (ex) { };
            event.caller = xcaller;
        }
    
        return this.each(function() {
            jQuery.event.trigger(event, data, this);
        });
    };
    
    
    jQuery.fn.click = function(fn) {
    
        var returned = null;
    
        if (fn) {
            returned = this.bind('click', fn)
        } else {
            var event = jQuery.Event('click'), xcaller = "";
            try {
                xcaller = arguments.callee.caller;
            } catch (ex) { };
            event.caller = xcaller;
            returned = this.trigger(event);
        }
    
        return returned;
    };
    
    3 回复  |  直到 15 年前
        1
  •  4
  •   Community CDub    8 年前

    你可以在触发一个人工事件时传递数据,关键是你不能使用像 click() ,而是使用 trigger 直接:

    $("#mybutton").tigger('click', ['One', 'Two']);
    

    单击处理程序:

    $("#mybutton").click(function(ev, customArg1, customArg2) {
      customArg1; // "One"
      customArg2; // "Two"
    })
    

    视若无睹 .click() 只是通往 .trigger('click') 不管怎样,你这样做不会失去任何东西,只会失去更多的钥匙。


    编辑寻址注释 :

    系统已经写入,并且 大到可以改变一切 脚本

    在这种情况下,您可能需要黑客jquery来捕获 arguments.callee.caller 并将其传递给您的处理程序:

    jQuery.fn.click = function(fn) {
      return fn ? this.bind(name, fn) : this.trigger(name, [arguments.callee.caller]);
    };
    

    有了那个补丁,调用的代码 点击() 现在直接将其调用函数作用域信息传递给您的单击处理程序,该处理程序现在可以执行以下操作:

    $("#mybutton").click(function(ev, caller) {
       if (caller === funOne || caller === funTwo){
         //my code
       }
    });
    

    如果什么 this says 是真的,那么 arguments.callee.caller 在将来是不可靠的,但后来,出于某种原因,黑客被称为黑客;)

        2
  •  0
  •   Community CDub    8 年前

    您正在查找arguments.callee。现在,表单javascript 1.4已被弃用。

    See here 了解更多详细信息。

        3
  •  0
  •   Reigel Gallarde    15 年前

    我相信,当你根据你的代码片段点击时,它总是会触发:

    $("#mybutton").click(function(ev){
    
       if (called from funcion One or Two){
         //my code
       }
    
    });
    

    而不是那些功能…(有人吗?如果我错了,请纠正我…)

    编辑…

    啊……我现在明白了…你想要动态点击…(在想…)