如何让event.srcElement在Firefox中运行,这是什么意思?

cod*_*441 80 javascript firefox cross-browser

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

if(event.srcElement.getAttribute("onclick") == null){ 
...code..
document.mainForm.submit();
}
Run Code Online (Sandbox Code Playgroud)

我已经注释掉了if语句条件,现在它正在使用forefox.我的问题是,event.srcElement.getAttribute("onclick")是什么,重要的是,它是否会在将来引发问题.还有,类似的东西,我可以替换条件,以便它适用于Firefox?

编辑:

 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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Fel*_*ing 172

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

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

if(target.onclick == null) { // shorter than getAttribute('onclick')
    //...
    document.mainForm.submit();
}
Run Code Online (Sandbox Code Playgroud)

另请参阅quirksmode.org -更多跨浏览器信息的事件属性.


关于它正在做什么的问题:

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

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


小智 5

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

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

}
Run Code Online (Sandbox Code Playgroud)

HTML:

<input type="text" id="txt_Name" onkeydown="toDoOnKeyDown(event);"/>
Run Code Online (Sandbox Code Playgroud)

当你注意到我们在html中调用函数时,我们已经添加了一个参数event,以防浏览器是Firefox.

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

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

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 -->
};
Run Code Online (Sandbox Code Playgroud)