onclick event not triggered when onchange triggered right before

前端 未结 3 684
情话喂你
情话喂你 2021-01-14 17:50

i have a funny problem here.

I have a textarea with an onchange event linked to it. Then i have a button linked to an onclick event.

The text being put to th

相关标签:
3条回答
  • 2021-01-14 18:29

    You need to recheck your assumptions. In your example, alert() interrupts the program flow, breaking handling of onclick. This code (jsfiddle here) demonstrates that onchange does not interfere with onclick by default:

    <textarea onchange="processText();" name="mytext"></textarea>
    <button onclick="processButton();">Hello</button>
    <div id="clicked"></div>
    <div id="changed"></div>
    <script language="Javascript">
      function processText()
      {
        document.getElementById('changed').innerHTML = "onchange fired";;
      }
    
      function processButton()
      {
        document.getElementById('clicked').innerHTML = "onclick fired";
      }
    </script>​
    

    I had a similar problem, but the actual reason that the onclick event was not handled was that the element being clicked, scrolled down as a result of the onchange handler. When the mouse button was released, the element did not receive a mouseup event and the "click" was interrupted.

    0 讨论(0)
  • 2021-01-14 18:39

    handle onblur event on textarea and onclick event on button

    0 讨论(0)
  • 2021-01-14 18:39

    try this to add event listener to components

    document.getElementsByTagName("textarea")[0].addEventListener("change", function(){
        alert( 'textarea');
    });
    
    document.getElementsByTagName("button")[0].addEventListener("click", function(){
        alert( 'button');
    });
    

    if Ids are defined then use getElementById().

    0 讨论(0)
提交回复
热议问题