javascript - onBlur event in IE - Stack Overflow

I have this HTML:<textarea class="question" onBlur="onBlurFun(event); return true;&qu

I have this HTML:

<textarea class="question" onBlur="onBlurFun(event); return true;" 
name="aaa" id="bbb"></textarea>

And this function in JavaScript

    onBlurFun = function(event) {
 var tgt = $(event.target);
 alert(tgt.attr('id'));
    }

In IE, tgt is not properly set. What should I do to have access to calling node?

I have this HTML:

<textarea class="question" onBlur="onBlurFun(event); return true;" 
name="aaa" id="bbb"></textarea>

And this function in JavaScript

    onBlurFun = function(event) {
 var tgt = $(event.target);
 alert(tgt.attr('id'));
    }

In IE, tgt is not properly set. What should I do to have access to calling node?

Share edited Oct 31, 2020 at 17:12 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 6, 2010 at 10:06 liysdliysd 4,64313 gold badges36 silver badges38 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

event.srcElement is Internet Explorer's equivalent of event.target. jQuery normalizes this for you in event handlers applied using .bind(), so you can use event.target in Internet Explorer, but event handlers applied through HTML attributes or the DOM properties won't have the property. In your case, you could change the code to

// event.target or event.srcElement
var tgt = $(event.target || event.srcElement);

or follow June R's advice and pass in a reference to the element instead.

As a side note, there is no need to return true; for most events, but especially for onblur and onfocus since you can't cancel the default action of them anyway.

To check target do not pass event intead of this pass object it self like

look this code

<textarea class="question" onBlur="onBlurFun(this); return true;" 
name="aaa" id="bbb"></textarea>

 function  onBlurFun(obj) 
{
   alert(obj.id); 
}

IE has its own global event object.

This should work

onBlurFun = function(event) {

   if (!event)
   {
     event = window.event;
     event.target = event.srcElement;
   }

   var tgt = $(event.target);
   alert(tgt.attr('id'));

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745055098a4608614.html

相关推荐

  • javascript - onBlur event in IE - Stack Overflow

    I have this HTML:<textarea class="question" onBlur="onBlurFun(event); return true;&qu

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信