Javascript 如何让 event.srcElement 在 Firefox 中工作,这意味着什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5301643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:33:39  来源:igfitidea点击:

How can I make event.srcElement work in Firefox and what does it mean?

javascriptfirefoxcross-browser

提问by code511788465541441

there is an if statement on my company's website that makes one web page imcompatible with firefox

我公司网站上有一个 if 语句,使一个网页与 firefox 不兼容

if(event.srcElement.getAttribute("onclick") == null){ 
...code..
document.mainForm.submit();
}

I've commented out the if statement conditions and now its working with forefox. My question is, what is event.srcElement.getAttribute("onclick"), is it important, would it cause problems in the future. also, is there something similar i can replace the condition with so that it works on firefox?

我已经注释掉了 if 语句条件,现在它可以与 forefox 一起使用。我的问题是,什么是 event.srcElement.getAttribute("onclick"),它是否重要,是否会在将来引起问题。另外,有没有类似的东西我可以用它来替换条件,以便它在 Firefox 上工作?

Edit:

编辑:

 function gotoRDManagerPT(PTId, bDDetailId) {
        if(!proceed()) return false;
        var target = event.target || event.srcElement; 
        if(event.target.getAttribute("onclick") == null) { 
            document.mainForm.displayRDManagerPT.value = "true";
            document.mainForm.PTId.value = PTId;
            document.mainForm.bDDetailId.value = bDDetailId;
            document.mainForm.submit();
        }
    }

回答by Felix Kling

srcElementis proprietary property originally coming from IE. The standardized property is target:

srcElement是最初来自 IE 的专有财产。标准化的属性是target

var target = event.target || event.srcElement;

if(target.onclick == null) { // shorter than getAttribute('onclick')
    //...
    document.mainForm.submit();
}

Also have a look at quirksmode.org - Event propertiesfor more cross browser information.

还可以查看quirksmode.org - 事件属性以获取更多跨浏览器信息。



Regarding the question what it is doing:

关于它在做什么的问题:

event.target/ event.srcElementcontains a reference to the element the eventwas raised on. getAttribute('onclick') == nullchecks whether a click event handler is assigned to element via inline event handling.

event.target/event.srcElement包含对event引发的元素的引用。getAttribute('onclick') == null检查是否通过内联事件处理将单击事件处理程序分配给元素。

Is it important? We cannot say because we don't know what the ...code..is doing.

那很重要么?我们不能说,因为我们不知道...code..它在做什么。

回答by Marwan

In IE the event object is available in the window object already; in Firefox, it's passed as a parameter in the event handler.

在 IE 中,事件对象已经在 window 对象中可用;在 Firefox 中,它在事件处理程序中作为参数传递。

Example

例子

JavaScript:

JavaScript:

function toDoOnKeyDown(evt)

{

    //if window.event is equivalent as if thie browser is IE then the event object is in window
    //object and if the browser is FireFox then use the Argument evt

    var myEvent = ((window.event)?(event):(evt));
    //get the Element which this event is all about 

    var Element = ((window.event)?(event.srcElement):(evt.currentTarget));
    //To Do -->

}

HTML:

HTML:

<input type="text" id="txt_Name" onkeydown="toDoOnKeyDown(event);"/>

As you notice when we called the function inside the html we have added a parameter eventjust in case the browser is Firefox.

正如您注意到的,当我们在 html 中调用该函数时,我们添加了一个参数event,以防浏览器是 Firefox。

I have read in an article that the event object in IE is called window.eventand in Firefox we have to put it as a parameter.

我在一篇文章中读到 IE 中的事件对象被调用,window.event而在 Firefox 中我们必须将其作为参数。

In case you need it to be attached in the code:

如果您需要将其附加到代码中:

document.getElementById('txt_Name').onkeydown = function(evt) {
    var myEvent = ((window.event)?(window.event):(evt));


    // get the Element which this event is all about 

    var Element = ((window.event)?(event.srcElement):(evt.currentTarget));
    // To Do -->
};

回答by Pankaj Shrivastava

Try quick fix as follows:

尝试快速修复如下:

Include in code:

包含在代码中:

let target = event.target || event.srcElement;

and change

和改变

event.srcElement.XXXXX to target.XXXXX

this solves the issue with Firefox.

这解决了 Firefox 的问题。