Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
301 views
Welcome To Ask or Share your Answers For Others

1 Answer

  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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...