4

I have an event listener like this:

div.addEventListener('mouseover',function(){bubble_info.call(data);},false);

function bubble_info(e,info){
  //get e.pageX etc
  //do stuff with info
}

This problem is in bubble_info the variable e holds the info of data and info is undefined

How do i make sure i can get e and info correctly?

4

2 回答 2

1

事件对象有很多有用的属性和方法

div.addEventListener('mouseover',function(event){
    bubble_info(event, info); 
    // you can pass additional params that can be used in your handler 
},false);

function bubble_info(event, info){
  // you can access type of event from event object's properties
  console.log(event.type);
  console.log(info); // your additional parameter.
};

addEventListener 文档

仅当您需要传递this (当前)对象的引用时才使用调用。它的语法将是...

FunctionName.call(thisArg, arguments-list, ...);

致电文档

于 2013-07-31T06:09:19.907 回答
0

试试这个(你不需要调用,除非你传递一个特定的this引用——无论如何你的代码中都缺少它):

div.addEventListener( 
  'mouseover', 
  function(event) { 
    bubble_info( event, dataYouWant );
  }, 
  false 
}

作为参考, .call() 应该如下所示:bubble_info.call( this, event, etc )

于 2013-07-31T06:21:53.007 回答