开发者

Get the current field name

I called below function on the body and what I want is when I press anything in textbox it will alert me the name of the textbox and I want to check the name and then execute something. Checking name is working great in FF and not in IE. Thanks

<body bgcolor="#F2F2F2" OnKeyPress="return getFiel开发者_高级运维dName(event);">

...

function getFieldName(e) {
    e = e || window.event;
    var key = e.keyCode || e.which,
    target = e.target || e.srcElement;
}

This works but target.name is not trappable in IE for eg:

if (target.name == 'one') {
    //we can reach here is FF and Not in IE
}


You can know which element triggered the event by looking the e.target property (or e.srcElement for IE):

function getFieldName(e) {
  e = e || window.event;
  var key = e.keyCode || e.which,
      target = e.target || e.srcElement;

  alert(target.name);
  return (key != 13);
}

Check the above example here.

And give a look to this article:

  • JavaScript Event Delegation is Easier than You Think
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜