如何在onChange之前触发onClick事件(即使值已更改)? [英] How to fires onClick event before onChange (even if the value has changed)?

查看:512
本文介绍了如何在onChange之前触发onClick事件(即使值已更改)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,抱歉我的英语.

Hello everyone and sorry for my english.

在此 jdfiddle 之后,我遇到了一个我不明白的问题.

Following this jdfiddle, I have a problem I don't understand.

首先,我更改输入的值,然后单击按钮,我希望看到的是单击",但是我看到更改",意思是onclickonchange之后被调用.

First I change the value of the input, then I click on the button and what I expected to see is "click" but I see "change" meaning onclick is called after onchange.

<input type="text" onChange="alert('change')" />
<button type="button" onClick="alert('click');">Click!</button>

我在此帖子中发现

您有个好的方法吗?

推荐答案

好的,所以我知道您真的不想这样做. 您可以先运行on change函数,然后再查看具有焦点的内容,如果是该按钮,您将知道它已被单击,因此是失去焦点的原因.

Ok so I can see that you really wan't this. You could let the on change function run first, and then afterward see what has focus, if it is that button, you would know that it was clicked, and thus was the reason for loosing focus.

在运行更改功能时,您无法检查此内容,因为此时主体仍具有焦点.

you cannot check this while the change function is running, as the body still has focus at this time..

<head>
  <script language="javascript">
  function focussed()
  {
    var triggerElement = document.activeElement
    alert(triggerElement)
    console.info(triggerElement)
  }

  function change()
  {
    alert("change")
    setTimeout(focussed, 1)
    return true
  }
  </script>

</head>
<body>
    <input type="text" onChange="alert('change')" />
    <button type="button" onClick="alert('click');">Click!</button>
</body>

这篇关于如何在onChange之前触发onClick事件(即使值已更改)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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