如何在IE中同步触发元素事件? [英] How Trigger Element event synchronously in IE?

查看:64
本文介绍了如何在IE中同步触发元素事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre class =snippet-code-js lang-js prettyprint-override> $(function(){$('#btn')。click(function(){$('#inp') .focus(); console.log('two');}); $('#inp')。focus(function(){console.log('one');});});

 < script src =https://ajax.googleapis.com/ ajax / libs / jquery / 2.1.1 / jquery.min.js>< / script>< button id =btn>点击< / button>< input type =textid =inp>  



如果您在Chrome中运行上述代码段,那么输出将是

 一个
两个

当你在IE中运行时,输出是

 两个
一个

如何在IE中使它同步?

解决方案

解决方案是使用 triggerHandler 方法。


.triggerHandler()返回返回的任何值它导致执行的最后一个
处理程序。如果没有重新触发处理程序,则
返回undefined


console.log('two') 仅在焦点事件结束时运行。

  $(function(){
$('#btn')。click(function(){
$('#inp')。triggerHandler('focus');
console.log('two');
});
$('#inp')。focus(function(){
console.log('one');
});
});


$(function() {
  $('#btn').click(function() {
    $('#inp').focus();
    console.log('two');
  });
  $('#inp').focus(function() {
    console.log('one');
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn">
            Click
        </button>
<input type="text" id="inp">

If you run the above snippet in Chrome the out put will be

one
two

When you run in IE the out put is

two
one

How to make it synchronous in IE?

解决方案

The solution is to use triggerHandler method.

.triggerHandler() returns whatever value was returned by the last handler it caused to be executed. If no handlers re triggered, it returns undefined

console.log('two') will only run when focus event is finished.

$(function() {
  $('#btn').click(function() {
    $('#inp').triggerHandler('focus');
    console.log('two');
   });
  $('#inp').focus(function() {
    console.log('one');
  });
});

这篇关于如何在IE中同步触发元素事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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