Why is the onClick event triggered twice?

前端 未结 10 1241
夕颜
夕颜 2021-02-05 02:40

I have the following html:


    
&         


        
10条回答
  •  抹茶落季
    2021-02-05 03:00

    It is calling twice because button is inside a span and span has onclick="alert('Boem')", hence when you trigger click on button then it shows alert and same click event propagate to span and shows alert once again.

    you need to stop default behaviour of button using below code :

    $(function(){
    $('#test1').click(function(e){e.preventDefault();}).click();
    });
    

    Demo

提交回复
热议问题