简体   繁体   English

IE8 中的 JavaScript 事件原型

[英]JavaScript Event prototype in IE8

I'm trying to add a method to the Event prototype.我正在尝试向事件原型添加一个方法。 In order to call/set preventDefault() or, in IE-speak returnValue = false and -if desired- stopPropagation() / cancelBubble = true;为了调用/设置preventDefault()或者,在 IE 中returnValue = false和 - 如果需要 - stopPropagation() / cancelBubble = true; . . I thought the code below would have sufficed.我认为下面的代码就足够了。

Event = Event || window.Event;
//^^ makes the fiddle work on IE8 ^^
if(!(Event.prototype.stopEvent))
{
    Event.prototype.stopEvent = function(propagate)
    {
        "use strict";
        propagate = (propagate ? true : false);
        if (this.preventDefault)
        {
            this.preventDefault();
            if (propagate === false)
            {
                this.stopPropagation();
            }
        }
        else
        {
            this.returnValue = false;
            this.cancelBubble = !propagate;
        }
        return this;
    };
}

Which seems to work, as you can see here .正如您在此处看到的那样,这似乎有效。 This fiddle shows OK in IE8, firefox and chrome.这个小提琴在 IE8、firefox 和 chrome 中显示OK Though, when I add this to my script, IE8 breaks on the first line, saying 'Event is undefined' .但是,当我将它添加到我的脚本中时,IE8 在第一行中断,说'Event is undefined' Leaving out "use strict";省略"use strict"; makes no difference at all.根本没有区别。

Reluctantly, I tried this, too:不情愿地,我也试过这个:

if (typeof Event === 'undefined')
{
    var Event = window.Event || window.event;//FFS IE :-(
}

But to no avail: Error: 'Event.prototype' is null or not an object , so I got 1 line further.但无济于事: Error: 'Event.prototype' is null or not an object ,所以我又得到了 1 行。 The thing is, the entire prototype method is a copy paste from my script, but what am I overlooking here?问题是,整个原型方法是从我的脚本复制粘贴,但我在这里忽略了什么? Any idea's/suggestions?任何想法/建议?
Thanks谢谢

PS: I like Pure JavaScript, so please, don't suggest jQuery, prototypejs, dojo,... as a solution. PS:我喜欢纯 JavaScript,所以请不要建议使用 jQuery、prototypejs、dojo...作为解决方案。 I've just gotten rid of jQuery.我刚刚摆脱了 jQuery。 (I like jQuery, but there is no need for it in this case) (我喜欢 jQuery,但在这种情况下不需要它)


Update更新

Things have taken a turn for the worse, I'm afraid.恐怕事情变得更糟了。 I found this MSDN reference .我找到了 这个 MSDN 参考 The entire page deals with DOM Element prototypes.整个页面处理 DOM 元素原型。 It's pretty fair to say they are available and usable in IE8 (to some extent).可以很公平地说它们在 IE8 中可用(在某种程度上)。 On this page, this code caught my eye:在这个页面上,这段代码引起了我的注意:

Event.prototype.stopPropagation = function ()
{
  this.cancelBubble = true;
};
Event.prototype.preventDefault = function ()
{
  this.returnValue = false;
};

It can be found ~3/4ths of the page down, in the section titled "Powerful Scenarios" .它可以在页面下方的大约 3/4 处找到,在标题为"Powerful Scenarios"的部分中。 This is, to my mind exactly the same thing as I want to do, but what's more: if I try this code via jsfiddle, it doesn't even work, whereas my jsfiddle (with my code) did work on IE8.在我看来,这与我想要做的完全相同,但更重要的是:如果我通过 jsfiddle 尝试此代码,它甚至不起作用,而我的 jsfiddle(使用我的代码)在 IE8 上运行。 This is just the last few lines of a snippet, but as far as I can work out, these few lines of code should work just fine.这只是片段的最后几行,但据我所知,这几行代码应该可以正常工作。 I've altered them as follows:我已将它们更改如下:

Event.prototype.stopPropagation = function ()
{
    if (this.stopPropagation)
    {
        return this.stopPropagation();
    }
    this.cancelBubble = true;
};
Event.prototype.preventDefault = function ()
{
    if (this.preventDefault)
    {
        return this.preventDefault();
    }
    this.returnValue = false;
};

I recently had (another) brainwave, and I think I found a better way of augmenting the event prototype.我最近有(另一个)脑电波,我想我找到了一种更好的方法来增强事件原型。 Strictly speaking, the Event prototype is not accessible in IE (<9), but it is, I found out accessible if you work your way back from an instance (well, the instance: window.event ).严格来说, Event原型在 IE (<9) 中是不可访问的,但是,如果你从一个实例(好吧,实例: window.event )返回,我发现它是可以访问的。 So here's a snippet that works in all major browsers (and IE8 - not 7):所以这是一个适用于所有主要浏览器(和 IE8 - 而不是 7)的片段:

(function()
{
        function ol(e)
        {//we have an event object
            e = e || window.event;
            if (!e.stopEvent)
            {
                if (Object && Object.getPrototypeOf)
                {//get the prototype
                    e = Object.getPrototypeOf(e);
                }
                else
                {//getting a prototype in IE8 is a bit of a faff, this expression works on most objects, though
                 //it's part of my custom .getPrototypeOf method for IE
                    e = this[e.constructor.toString().match(/(function|object)\s+([A-Z][^\s(\]]+)/)[2]].prototype;
                }
                e.stopEvent = function(bubble)
                {//augment it (e references the prototype now
                    bubble = bubble || false;
                    if (this.preventDefault)
                    {
                        this.preventDefault();
                        if (!bubble)
                        {
                            this.stopPropagation();
                        }
                        return this;
                    }
                    this.returnValue = false;
                    this.cancelBubble = !bubble;
                    return this;
                };
            }
            alert(e.stopEvent ? 'ok' : 'nok');//tested, it alerts ok
            if (this.addEventListener)
            {
                this.removeEventListener('load',ol,false);
                return;
            }
            document.attachEvent('onkeypress',function(e)
            {
                e = e || window.event;
                if (e.stopEvent)
                {//another event, each time alerts ok
                    alert('OK!');
                }
            });
            this.detachEvent('onload',ol);
        }
        if (this.addEventListener)
        {
            this.addEventListener('load',ol,false);
        }
        else
        {
            this.attachEvent('onload',ol);
        }
})();

That way, the header doctype doesn't matter all that much: I've tested it using the <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> , and it works in FF, chrome and IE 8, no problems whatsoever.这样,标题文档类型就没有那么重要了:我已经使用<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ,它适用于 FF、chrome 和 IE 8,没有任何问题。 Using <!DOCTYPE html> to be safe, though不过使用<!DOCTYPE html>是安全的

Hope this helps someone...希望这可以帮助某人...

Its Standards versus Quirks mode.它的标准与怪癖模式。 The JSFiddle page has a DOCTYPE declaration, albeit an incredibly simple one, <!DOCTYPE html> , that kicks the render into Standards mode. JSFiddle 页面有一个 DOCTYPE 声明,尽管它是一个非常简单的<!DOCTYPE html> ,它将渲染踢入标准模式。 Chances are your web page does not have a DOCTYPE which leaves the render in Quirks mode.很可能您的网页没有 DOCTYPE,这会使渲染处于 Quirks 模式。 After adding that simple DOCTYPE to a page I built from your fiddle, it worked for me.将那个简单的 DOCTYPE 添加到我用您的小提琴构建的页面后,它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM