开发者

window.event.srcElement doesn't work for 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
 开发者_Python百科  while ( (current = current.parentElement) && current.tagName !="TR");
        current.parentElement.removeChild(current);
}


  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.

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
}


You have to use event.target instead of window.event.target to work for Firefox. Try to use the following.

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

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


var CurrentObject = 
    0 < window.navigator.appVersion.toString().indexOf("MSIE") 
    ? 
    window.event.srcElement 
    : 
    evt.target;


Here's Mozilla's documentation of the Event class; might give you some insight.


NEVER use individual browser checks like "check for MSIE".

Do you know how many browsers you'd have to check for... and individual browser VERSIONS... to check them all?

NEVER use jQuery for this either. Write correct code yourself. Never include (useless) massive libraries when you aren't going to use 99% of their features anyway.


just found out, works on ie & ff and chrome. others don't know but I expect that this will work on allso the rest. at least hope so :) As a beginner in html, JS and PHP, it's very annoying that there is not a standard in these things ! even styling a simple div element can be a headage because of the behavior is not allways the same on different browsers !?

function somefunc()
{
  var callerelement = arguments.callee.caller.arguments[0].target;
  alert(callerelement.id)
}  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜