日期:2014-05-16 浏览次数:20428 次
$(function () {
var str = "1,2";
var arr = str.split(',');
for (var i = 0; i < arr.length; i++) {
$("#a"+arr[i]).bind("click", function () {
alert(arr[i]);
});
}
})
$(function () {
var str = "1,2";
var arr = str.split(',');
for (var i = 0; i < arr.length; i++) {
$("#a"+arr[i]).bind("click", (function(_i){
return function () {
alert(_i);
}
})(arr[i]));
}
});
<button id="a1">a1</button>
<button id="a2">a2</button>
<script type="text/javascript">
//这个需要用到闭包
var str = "1,2";
var arr = str.split(',');
for (var i = 0; i < arr.length; i++) {
document.getElementById("a"+arr[i]).onclick=(function(n){
return function(){
alert(n);
}
})(arr[i]);
}
</script>