English 中文(简体)
Java IE8的活动原型
原标题:JavaScript Event prototype in IE8

我试图在活动原型中添加一种方法。 为了打电话/打造preventDefault(,或, in IE-speakreturn 数值 = 虚假和――如果希望-stoppropagation(/cancelBubble = 真实;。 我认为以下法典已经足够了。

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;
    };
}

似乎在工作,,因为你可以在这里看到。 该网球显示:OK,载于IE8, 火灾fox and chrome。 虽然在我的发言中加上这一点,但IE8打破了第一行,说。 页: 1 删除<代码>“严格”;根本不适用。

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. 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:我像Pure Javas一样,不建议 j、原型、 do......作为解决办法。 I ve Just gotten rid of jQuery. ∗∗∗∗


Update

情况使情况恶化,我恐怕。 I found thiMSsDN reference。 整页处理OMElement原型。 可以说,这种说法是完全公平的,并且可以在第8号(在某种程度上)中使用。 在该网页上,这部法典引起了我的注意:

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

可在题为“代码”的栏目中查找下页数的~3/4。 我认为,这确实与我想要做的事情相同,但更重要的是:如果我通过黑手党来尝试这一法典,那就连工作了,而我的 j(有我的法典)确实在8国集团工作。 这只是最后几条刀子,但就我所能做的工作而言,这些几条法典应当只是罚款。 我将其改动如下:

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;
};
最佳回答

我最近曾(另一人)洗脑,我认为我找到了更好的办法,来扩大活动原型。 严格地说,<代码>Event原型无法在IEE(<9)上查阅,但是,如果你从一个实例(例如:window.event)上回路,则可以查阅。 因此,这里有一个在所有主要浏览器上运行的灯塔(和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);
        }
})();

这样一来,头脑膜炎就显得重要了: 我用<代码><对它进行了测试;! DOCTYPE html pub “-/W3C/DTD X/2006/2 1.0 Strict/EN”“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>,并在FF、chrome和IE 8,中开展工作,没有任何问题。 使用<条码>和;! DOCTYPE html> 是安全的,尽管如此

Hope this helps someone...

问题回答

其标准与基茨模式。 The JSFiddle page has a DOCTYPE declaration, albeit an incredibly brief one, <!DOCTYPE html>, that kicks the make into Standards manner. Chances是你的网页,没有DOCTYPE,而DOCTYPE在Qurks模式中留下来。 在用你手提的那页上添加了这一简单的“DOCTYPE”之后,它为我工作。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.