Created
February 22, 2012 08:39
-
-
Save honbin/1883443 to your computer and use it in GitHub Desktop.
クロージャのサンプル(ulのmemberクラスにli要素を生成して、クリックイベントをはる。jquery利用)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* HTMLはこんな感じ | |
* <html> | |
* <head> | |
* <meta charset="UTF-8" /> | |
* <title>クロージャの勉強</title> | |
* <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
* </head> | |
* <body> | |
* <ul class="member"></ul> | |
* </body> | |
* </html> | |
*/ | |
(function($) { | |
var members = [ | |
{name:"松本", comment:"イケメン"}, | |
{name:"竹本", comment:"エロデザイナー"}, | |
{name:"倉田", comment:"むっつり"}, | |
{name:"梨本", comment:"がち"}, | |
{name:"金子", comment:"・・・・"}, | |
]; | |
var handleClickOnMembers = function () { | |
var len = members.length; | |
var cnt; | |
for (cnt = 0; cnt < len; cnt += 1) { | |
$(".member").append("<li>" + members[cnt].name + "</li>"); | |
$(".member li")[cnt].onclick = function (cnt) { | |
return function () { | |
alert(members[cnt].comment); | |
} | |
}(cnt); | |
} | |
}(); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment