window.event.srcElement不适用于firefox?

DGT*_*DGT 3 javascript

以下不适用于Firefox.我正在尝试删除点击表格行.谁能请帮忙.非常感谢.

<INPUT TYPE="Button" onClick="delRow()" VALUE="Remove">

function delRow(){
   if(window.event){
      var current = window.event.srcElement;
   }else{
      var current = window.event.target;
   }
   //here we will delete the line
   while ( (current = current.parentElement) && current.tagName !="TR");
        current.parentElement.removeChild(current);
}
Run Code Online (Sandbox Code Playgroud)

air*_*x86 9

  1. window.event仅限IE.W3C标准中不存在window.event.
  2. 默认情况下,event对象作为具有W3C标准的事件处理程序的第一个参数传入.
  3. 标记中的内联onlick事件调用函数意味着事件处理程序正在调用该函数.以您的标记为例.这意味着function() { delRow(); }.如您所见event,delRow()除非您在IE中,因为事件在窗口对象中,否则您将无法看到该对象.
  4. parentElement也只是IE,在大多数情况下将其更改为parentNode将起作用.假设父节点也是一个元素.

我建议你使用jQuery这样的javascript库,或者如果你需要保持相对相同的东西就改变你的代码.

<INPUT TYPE="Button" onclick="delRow(event);" VALUE="Remove">

function delRow(e) {
    var evt = e || window.event; // this assign evt with the event object
    var current = evt.target || evt.srcElement; // this assign current with the event target
    // do what you need to do here
}
Run Code Online (Sandbox Code Playgroud)