0

我有一个关于多浏览器兼容性的问题。我想在以下代码中使用 event.target 而不是 event.srcElement 以使其适用于 firefox。

我用过 target = event.target || 事件.srcElement。它不工作。任何帮助将不胜感激。

function jumptoPopupMenuItem(theMenuID) 
{                
  if (event.srcElement.className == "RightClickMenuItems") 
  {
    if (event.srcElement.getAttribute("url") != null)
    {        
      var strParameters = "";                                    
      if (theMenuID == "mnuAppointmentMenu")
      {
        strParameters = "AppointmentNumber=" + m_strAppointmentTypeYearNumber;
      }
      else if (theMenuID == "mnuAvailableHourMenu")
      {
        strParameters = "PreFillLanguageID=" + m_nLanguageID;
        strParameters = strParameters + "&PreFillInterpreterID=" + m_nInterpreterID;
        strParameters = strParameters + "&PreFillDateOfService=" + m_dtDateOfService;
      }
      if (event.srcElement.getAttribute("target") != null)
      {
        var PopupWindow = window.open(
            event.srcElement.url + strParameters, 
            event.srcElement.getAttribute("target"));
        PopupWindow.focus();
      }
      else
      {
        window.location = event.srcElement.url;
      }
    }
    hidePopupMenu(theMenuID);
  }
}
4

1 回答 1

0

注册具有兼容性的事件处理程序的一些标准方法(非常简化的代码):

HTML:

<button onclick="eventHandlerFunc(event)" />
<!-- better to register as: -->
<button onclick="eventHandlerFunc.call(this,event)" />

JavaScript(addEventListener 或 attachEvent):

if (window.addEventListener) someElem.addEventListener("click",eventHandlerFunc,false);
else if (window.attachEvent) someElem.attachEvent("onclick",eventHandlerFunc);

JavaScript(元素属性):

someElem.onclick = eventHandlerFunc;

其中eventHandlerFunc()函数定义为:

function eventHandlerFunc(event) { // or var eventHandlerFunc = function(event) {
  event = event||window.event; // can be needed only for IE6-IE8
                               // because `event` parameter hide `event` global
                               // variable (`window.event`)
  var target = event.target||event.srcElement
}

注意:如果需要,this必须添加变量附加代码的兼容性。

如果需要在其他函数中使用event变量(在内部定义eventHandlerFunc()为参数),则必须将其发送到这些函数:

function eventHandlerFunc(event) {
  event = event||window.event; // can be needed only for IE6-IE8
  /*
  ...
  */
  jumptoPopupMenuItem(event,theMenuID);
}

function jumptoPopupMenuItem(event,theMenuID) {
}
于 2012-09-18T06:20:55.503 回答