window.event.srcElement不适用于firefox? [英] window.event.srcElement doesn't work for firefox?

查看:123
本文介绍了window.event.srcElement不适用于firefox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容对Firefox不起作用。我正在尝试删除点击表格行。谁能请帮忙。非常感谢。

The following doesn't work for firefox. I'm trying delete the table row on click. Can anyone please help. Many thanks.

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


推荐答案


  1. window.event仅限IE。 W3C标准中不存在window.event。

  2. 默认情况下,事件对象作为具有W3C标准的事件处理程序的第一个参数传入。

  3. 标记中的内联onlick事件调用函数意味着事件处理程序正在调用该函数。以您的标记为例。它的意思是 function(){delRow(); } 。如您所见,除非您在 delRow(),否则您将无法看到事件对象IE因为事件在窗口对象中。

  4. parentElement也只是IE,在大多数情况下将其更改为parentNode会起作用。假设父节点也是一个元素。

  1. window.event is IE only. window.event does not exist in W3C standard.
  2. event object by default is pass in as the first argument to a event handler with the W3C standard.
  3. an inline onlick event in the markup calling a function mean that the event handler is calling that function. With your markup as example. It mean function() { delRow(); }. As you can see you won't be able to see the event object in delRow() except when you are in IE because event is in the window object.
  4. parentElement is also IE only, in most case changing it to parentNode would work. Assuming the parent node is also an element.

我建议您使用jQuery等javascript库或者如果需要更改代码保持相对一致。

I suggest you to use javascript library such as jQuery or change your code if you need to keep things relatively the same.

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

这篇关于window.event.srcElement不适用于firefox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆