lundi 20 avril 2015

click function was executed everytime the document is loaded

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>click demo</title>
  <script src="http://ift.tt/1rfghyb"></script>
</head>
<body> 
<script>
    for(var c=1; c<=5; c++){
        var btn = $("<button>Button "+c+"</button>");
        btn.click(new function(){
            alert("You click button "+c);
        });
        $("body").append(btn);
   }
   </script>
</body>
</html>

Can someone explain to me why the click function get executed everytime the page is loaded? I am expecting the button to show an alertbox when it is clicked but unfortunately nothing happens! Any idea?

Aucun commentaire:

Enregistrer un commentaire