Wednesday, August 22, 2012

javascript – The difference between directly binding and using each function

I am really confused about each function.


JsFiddle



<html>
<head>
<script type=\"text/javascript\" src=\"jquery-1.8.0.min.js\"></script>
<script>
$(document).ready(function(){
//$(\".a\").click(function(){
// alert(\"hi\");
//});
$(\".a\").each(function(){
$(this).click(function(){
alert(\"hi\");
});
});
});
</script>
</head>
<body>
<div class=\"a\">hi</div>
<div class=\"a\">hi</div>
</body>
</html>


What\’s the difference between using .click function directly and using .each? Both of them works.


Thanks a lot.


EDIT


If these two methods are the same, could you please give an example why .each is necessary?






Rating: 4 out of 5 based on 3 ratings



The post javascript – The difference between directly binding and using each function appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/javascript-the-difference-between-directly-binding-and-using-each-function.html

No comments:

Post a Comment