jQuery/[removed] Defining a global variable within a function?

后端 未结 3 2083
庸人自扰
庸人自扰 2021-02-15 11:19

I have this code:

        var one;
        $(\"#ma1\").click(function() {
            var one = 1;
        })
        $(\"body\").click(function() {
                     


        
相关标签:
3条回答
  • 2021-02-15 11:53

    If you want to make a global variable bind it to window object

    window.one = 1;
    
    0 讨论(0)
  • 2021-02-15 11:55
        var one;//define outside closure
    
        $("#ma1").click(function() {
            one = 1; //removed var 
        })
        $("body").click(function(e) {
            $('#status').html("This is 'one': "+one);
        })
    
    0 讨论(0)
  • 2021-02-15 11:58

    Remove the var from inside the function.

        $("#ma1").click(function() {
            one = 1;
        })
    
    0 讨论(0)
提交回复
热议问题