Why the onclick element will trigger twice for label element

后端 未结 5 1907
时光说笑
时光说笑 2020-11-27 07:18



        
5条回答
  •  清酒与你
    2020-11-27 07:56

    The Label tag will be associated with the input tag inside it. So when you click the label, it will also trigger a click event for the input, then bubble to the label itself.

    See this:

    document.getElementById("winput").addEventListener('click', function(event){
         alert('input click');
    
         //stop bubble
         event.stopPropagation();
    
     }, false); 
    

    http://jsfiddle.net/96vPP/

提交回复
热议问题