我的任务是在不使用jQuery或prototype等任何库的情况下在文档上触发自定义事件。
所以我对Firefox这样做很好:
function fireCustomEvent(eventData)
{
if (document.createEvent) // Firefox
{
var event = document.createEvent('HTMLEvents'); // create event
event.initEvent('myCustomEvent', true, true ); // name event
event.data = eventData; // put my stuff on it
document.dispatchEvent(event); // fire event
}
else if (document.createEventObject) // IE
{
xxxxxxxxxxx
}
}
现在我可以像这样开火:
fireCustomEvent({
category: 'test',
value: 123
});
并像这样捕获它(这里我可以使用jQuery):
$(document).bind('myCustomEvent', function (event) {
doStuff(event);
});
我的问题是,我可以做些什么来使这项工作在IE上(换句话说,我把xxxxxxxxxxx放在哪里)?
我认为等效的IE应该是这样的:
var event = document.createEventObject();
event.data = eventData;
document.fireEvent('myCustomEvent', event);
但这不起作用。 IE让我只使用预定义的事件名称(onclick等),甚至其中一些不起作用(例如onmessage)
感谢任何帮助或想法!
答案 0 :(得分:25)
在没有任何框架的情况下在Javascript中添加/删除/触发事件/自定义事件:
var htmlEvents = {// list of real events
//<body> and <frameset> Events
onload:1,
onunload:1,
//Form Events
onblur:1,
onchange:1,
onfocus:1,
onreset:1,
onselect:1,
onsubmit:1,
//Image Events
onabort:1,
//Keyboard Events
onkeydown:1,
onkeypress:1,
onkeyup:1,
//Mouse Events
onclick:1,
ondblclick:1,
onmousedown:1,
onmousemove:1,
onmouseout:1,
onmouseover:1,
onmouseup:1
}
function triggerEvent(el,eventName){
var event;
if (typeof window.CustomEvent === 'function') {
event = new CustomEvent(eventName);
} else if (document.createEvent) {
event = document.createEvent('HTMLEvents');
event.initEvent(eventName,true,true);
}else if(document.createEventObject){// IE < 9
event = document.createEventObject();
event.eventType = eventName;
}
event.eventName = eventName;
if(el.dispatchEvent){
el.dispatchEvent(event);
}else if(el.fireEvent && htmlEvents['on'+eventName]){// IE < 9
el.fireEvent('on'+event.eventType,event);// can trigger only a real event (e.g. 'click')
}else if(el[eventName]){
el[eventName]();
}else if(el['on'+eventName]){
el['on'+eventName]();
}
}
function addEvent(el,type,handler){
if(el.addEventListener){
el.addEventListener(type,handler,false);
}else if(el.attachEvent && htmlEvents['on'+type]){// IE < 9
el.attachEvent('on'+type,handler);
}else{
el['on'+type]=handler;
}
}
function removeEvent(el,type,handler){
if(el.removeEventListener){
el.removeEventListener(type,handler,false);
}else if(el.detachEvent && htmlEvents['on'+type]){// IE < 9
el.detachEvent('on'+type,handler);
}else{
el['on'+type]=null;
}
}
var _body = document.body;
var customEventFunction = function(){
console.log('triggered custom event');
}
// Subscribe
addEvent(_body,'customEvent',customEventFunction);
// Trigger
triggerEvent(_body,'customEvent');
答案 1 :(得分:11)
好根据dean edwards的那篇文章,我写了这个似乎有效,但它似乎是一个但很hacky。下面的示例基于#two嵌套在#one中的元素,因此我们可以模拟事件冒泡,因为我看不到或找不到在IE中冒泡自定义事件的方法。
function bindCustomEvent (el, eventName, eventHandler) {
if (el.attachEvent) {
if (!el[eventName]) {
el[eventName] = 0;
}
el.attachEvent("onpropertychange", function (event) {
if (event.propertyName == eventName) {
eventHandler(eventHandler);
}
});
}
}
bindCustomEvent(document.documentElement, "fakeEvents", function (evt) {
alert('document');
});
bindCustomEvent(document.getElementById('one'), "fakeEvents", function (evt) {
alert('one');
});
bindCustomEvent(document.getElementById('two'), "fakeEvents", function (evt) {
alert('two');
});
dispatchFakeEvent = function (el, eventName, bubble) {
bubble = !bubble ? false : true;
if (el.nodeType === 1 && el[eventName] >= 0) {
el[eventName]++;
}
if (bubble && el !== document) {
dispatchFakeEvent(el.parentNode, eventName, bubble);
}
};
document.getElementById('click').attachEvent('onclick', function () {
dispatchFakeEvent(document.getElementById('two'), 'fakeEvents', true);//false = bubble
});
答案 2 :(得分:3)
Prototype使用ondataavailable事件在IE中触发自定义事件