jQuery在自定义事件中为某些属性名称吃掉我的数据

tim*_*eam 4 jquery

这样做时:

$(document.body).on(
  'myevent',
  function (ev) {
    console.log(ev.data, ev.result);
  }
);

$(document.body).trigger(
  $.Event(
    'myevent',
    {
      data: 'foo',
      result: 'bar'    
    }
  )
);
Run Code Online (Sandbox Code Playgroud)

控制台返回[null, undefined].这有什么期待吗?我发现它很麻烦,因为没有原始事件要复制属性,我用完了属性名称来发送服务器响应.

参考:http://api.jquery.com/trigger/

参考:http://api.jquery.com/category/events/event-object/

注意文档描述了这种行为.如果有人知道为什么并从jQuery源代码解释它会很棒; 如果出现意外情况,我们可以提交错误.

Raf*_*fay 5

怎么样

$(document).on('myevent',"body",function (ev,x) {
    console.log(x.data);
    console.log(x.result);   
  }
);
$(document.body).trigger('myevent',{data:'foo',result: 'bar'});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/ZbBAh/17/


Dav*_*vin 5

ev.data属性是.on()调用中传递的数据,并且没有任何传递,因此它返回null.您在调用中添加的数据属性将$.Event被覆盖null.不要将此处提到的任何属性名称用于自定义数据名称:http: //api.jquery.com/category/events/event-object/