如何在Javascript中触发个人事件

时间:2022-12-03 20:35:38

I can't fire personal events using Javascript in IE. In Firefox work great.

我不能在IE中使用Javascript触发个人事件。在Firefox中伟大的工作。

My code is:

我的代码是:

var evento; 
if(document.createEventObject)  
{  
   evento = document.createEventObject();  
   document.fireEvent('eventoPersonal', evento);     
}  
//FF  
else  
{  
    evento = document.createEvent('Events');  
    evento.initEvent('eventoPersonal',true,false);  
    document.dispatchEvent(evento);  
}

But when try to execute document.fireEvent('eventoPersonal', evento); in IE, it doesn't work. How can I fire NO custom events in IE?

但是当尝试执行文档时。fireEvent(eventoPersonal,evento);在IE中,它不起作用。我怎样才能在IE中不使用自定义事件?

In Internet Explorer I get the error: "Invalid arguments" in the line where execute document.fireEvent('eventoPersonal', evento);

在Internet Explorer中,我得到的错误是:执行文档的行中的“无效参数”。fireEvent(eventoPersonal,evento);

6 个解决方案

#1


1  

You may want to consider using a library to abstract this. Both prototype an jquery will handle this for you. Jquery is especially good at allowing you to create an event with very simple code.

您可能想要考虑使用一个库来抽象它。这两个原型jquery都将为您处理这个问题。Jquery尤其擅长用非常简单的代码创建事件。

Jquery's documentation is available here: http://docs.jquery.com/Events

Jquery文档可以在这里找到:http://docs.jquery.com/Events

#2


39  

Dean Edward's describes how to fire cutsom events in IE

迪安·爱德华描述了如何在IE中发起活动

http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/

http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/

Its near the bottom of the article

在这篇文章的底部

var currentHandler;

if (document.addEventListener) {

  // We've seen this code already

} else if (document.attachEvent) { // MSIE

  document.documentElement.fakeEvents = 0; // an expando property

  document.documentElement.attachEvent("onpropertychange", function(event) {
    if (event.propertyName == "fakeEvents") {
      // execute the callback
      currentHandler();
    }
  });

  dispatchFakeEvent = function(handler) {
    // fire the propertychange event
    document.documentElement.fakeEvents++;
  };
}

#3


2  

I think the answer is - in IE you can not fire events that are not on this list:

我认为答案是——在IE中你不能触发不在这个列表上的事件:

MSDN - DHTML Events

MSDN——DHTML事件

From what I can gather, frameworks store a registry of the "custom" event names and you must use their implementation specific trigger and handle functions for custom events. For example, prototype uses the ondatavailable event to pass through their custom events behind the scenes.

根据我的收集,框架存储“自定义”事件名称的注册表,您必须使用它们的实现特定的触发器,并为自定义事件处理函数。例如,prototype使用ondatavailable事件在幕后传递它们的自定义事件。

#4


1  

In IE11 document.dispatchEvent still doesn't work, but now attachEvent is missing too, so the other solution is not going to work either. However, I came up with one even uglier. :) It involves replacing the addEventListener method and goes on like this:

在IE11文档。dispatchEvent仍然不能工作,但是现在attachEvent也消失了,所以另一个解决方案也不能工作。然而,我想到了一个更丑的人。它涉及到替换addEventListener方法并继续这样做:

var oldEventListener = document.addEventListener;

document.addEventListener = function (event, func, capture) {
    if (event == "MyPreciousCustomEvent") {
        document.MyPreciousCustomEvent = func;
    }

    oldEventListener.call(document, event, func, capture);
};

...

$(function () {
    try {
        document.MyPreciousCustomEvent("MyPreciousCustomEvent", {});
    } catch (e) {}
});

Hope this helps someone.

希望这可以帮助别人。

#5


0  

As I read the relevant MSDN article page on the createEventObject method, it appears as though it isn't used for creating custom event - it is used for creating custom objects that can be passed to already existing events.

当我在createEventObject方法上阅读相关的MSDN文章页面时,它似乎不用于创建自定义事件——它用于创建可传递给已有事件的自定义对象。

Description: Generates an event object to pass event context information when you use the fireEvent method.

Description:在使用fireEvent方法时,生成一个事件对象来传递事件上下文信息。

http://msdn.microsoft.com/en-us/library/ms536390%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms536390%28VS.85%29.aspx

Update: You are getting the "invalid arguments" error because 'eventoPersonal' is not an acceptable event to fire.

更新:您将获得“无效参数”错误,因为“eventoPersonal”不是一个可以触发的事件。

#6


0  

Yeah referring to @Don Albrecht, you can use jquery trigger() method more on http://api.jquery.com/trigger/

是的,引用@Don Albrecht,您可以在http://api.jquery.com/trigger/上更多地使用jquery触发器()方法

#1


1  

You may want to consider using a library to abstract this. Both prototype an jquery will handle this for you. Jquery is especially good at allowing you to create an event with very simple code.

您可能想要考虑使用一个库来抽象它。这两个原型jquery都将为您处理这个问题。Jquery尤其擅长用非常简单的代码创建事件。

Jquery's documentation is available here: http://docs.jquery.com/Events

Jquery文档可以在这里找到:http://docs.jquery.com/Events

#2


39  

Dean Edward's describes how to fire cutsom events in IE

迪安·爱德华描述了如何在IE中发起活动

http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/

http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/

Its near the bottom of the article

在这篇文章的底部

var currentHandler;

if (document.addEventListener) {

  // We've seen this code already

} else if (document.attachEvent) { // MSIE

  document.documentElement.fakeEvents = 0; // an expando property

  document.documentElement.attachEvent("onpropertychange", function(event) {
    if (event.propertyName == "fakeEvents") {
      // execute the callback
      currentHandler();
    }
  });

  dispatchFakeEvent = function(handler) {
    // fire the propertychange event
    document.documentElement.fakeEvents++;
  };
}

#3


2  

I think the answer is - in IE you can not fire events that are not on this list:

我认为答案是——在IE中你不能触发不在这个列表上的事件:

MSDN - DHTML Events

MSDN——DHTML事件

From what I can gather, frameworks store a registry of the "custom" event names and you must use their implementation specific trigger and handle functions for custom events. For example, prototype uses the ondatavailable event to pass through their custom events behind the scenes.

根据我的收集,框架存储“自定义”事件名称的注册表,您必须使用它们的实现特定的触发器,并为自定义事件处理函数。例如,prototype使用ondatavailable事件在幕后传递它们的自定义事件。

#4


1  

In IE11 document.dispatchEvent still doesn't work, but now attachEvent is missing too, so the other solution is not going to work either. However, I came up with one even uglier. :) It involves replacing the addEventListener method and goes on like this:

在IE11文档。dispatchEvent仍然不能工作,但是现在attachEvent也消失了,所以另一个解决方案也不能工作。然而,我想到了一个更丑的人。它涉及到替换addEventListener方法并继续这样做:

var oldEventListener = document.addEventListener;

document.addEventListener = function (event, func, capture) {
    if (event == "MyPreciousCustomEvent") {
        document.MyPreciousCustomEvent = func;
    }

    oldEventListener.call(document, event, func, capture);
};

...

$(function () {
    try {
        document.MyPreciousCustomEvent("MyPreciousCustomEvent", {});
    } catch (e) {}
});

Hope this helps someone.

希望这可以帮助别人。

#5


0  

As I read the relevant MSDN article page on the createEventObject method, it appears as though it isn't used for creating custom event - it is used for creating custom objects that can be passed to already existing events.

当我在createEventObject方法上阅读相关的MSDN文章页面时,它似乎不用于创建自定义事件——它用于创建可传递给已有事件的自定义对象。

Description: Generates an event object to pass event context information when you use the fireEvent method.

Description:在使用fireEvent方法时,生成一个事件对象来传递事件上下文信息。

http://msdn.microsoft.com/en-us/library/ms536390%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms536390%28VS.85%29.aspx

Update: You are getting the "invalid arguments" error because 'eventoPersonal' is not an acceptable event to fire.

更新:您将获得“无效参数”错误,因为“eventoPersonal”不是一个可以触发的事件。

#6


0  

Yeah referring to @Don Albrecht, you can use jquery trigger() method more on http://api.jquery.com/trigger/

是的,引用@Don Albrecht,您可以在http://api.jquery.com/trigger/上更多地使用jquery触发器()方法