循环警报无效

时间:2015-12-07 10:13:12

标签: javascript jquery html

我做了onclick警报,这对于简单的警报是好的,但我尝试用循环警报,它只能工作一次循环完成。在第二次点击它不能显示警告框... !! !

 <ul>
    <li>Click one</li>
    <li>Click two</li>
</ul>

<script>
    var n = 1;
    var txt = "Now you can see me";
    $(document).ready(function() {
        //$(document).unbind('click').bind('click', function(){
        $(document).off('click').on('click', function() {
            for (n; n <= 10; n++) {
                alert(txt + " for " + n + " times"); //after finished loop can't show alert
            }
        });
    });
</script>

我尝试过使用unbind和off trigger()?为什么第二次点击不起作用?

1 个答案:

答案 0 :(得分:2)

您需要在重新开始之前将n初始化为1.

   <script>
    var n = 1;
    var txt = "Now you can see me";
    $(document).ready(function() {
        //$(document).unbind('click').bind('click', function(){
        $(document).off('click').on('click', function() {
            for (n = 1; n <= 10; n++) {
                alert(txt + " for " + n + " times"); //after finished loop can't show alert
            }
        });
    });
</script>