【问题标题】:how to get click count outside of the function如何在函数之外获取点击次数
【发布时间】:2014-10-29 05:55:45
【问题描述】:

你好,这是我正在玩的代码

//define a function
var test=function() {

 var v=0;
 var counter=0;
  // click on the element id=z2
 $('#z2').click(function(){

  v = ++counter;
   //call function testdata 
  testdata();

  })

  function testdata() {

   return v;

   } // end of testdata

   } // end of  var test

     //calling p outside of the scope of var test.
    var p =test.testdata();

现在 p 应该返回 v 的值,但它在 console.log 中显示为 undefined。

请给点建议

【问题讨论】:

  • 您的代码中有未注释的文本行。我不确定这是否是原件,但请正确格式化。
  • 能否请您简要介绍一下您的具体要求,是否可以通过其他方式实现。
  • 我的主要目的是对表格数据进行分页。我用图表一次显示 10 条记录。用户单击 (#z2) 并因此显示更多 10 条记录,直到我们到达记录计数的末尾。比我们反过来做。因此需要捕捉点击的价值。我传递了很多参数来显示表格和图表,以及点击次数。由于关闭,表中下一个大项目的所有值都相同。因此需要捕捉点击次数。如果需要,我可以上传更多代码。我很难过。建议的解决方案是不充分的。 $.click() 不返回值。

标签: javascript jquery function closures


【解决方案1】:

您的代码不起作用的原因是因为 testdata 仅存在于函数 test 的范围内(使其成为私有函数) - 在 test 内调用它会起作用,但就剩下的代码是有关的,它不存在。

实现这项工作的一种方法是使用test 作为构造函数,保持变量私有但使函数公开,如下所示:

// define constructor
var Test = function() {

    // private
    var v = 0;
    var counter = 0;
    var that = this;

    // click on the element id=z2
    $('#z2').click(function(){
        v = ++counter;
        //call function testdata 
        that.testdata();
    });

    // public
    this.testdata = function() {
        return v;
    }
}

var p = new Test();
p.testdata(); // works!

这里的工作演示:http://jsfiddle.net/fissioncat/tg84mbwp/4/

【讨论】:

    【解决方案2】:

    你需要像这样将 v 定义为 int

    var v = 0;
    

    此外,该函数将返回未定义,因为该函数仅返回未定义的参数

    我很难理解您的问题,但我会这样做:

    //define a function
    var test = function() {
    
        var v = 0;
        var counter=0;
        // click on the element id=z2
        $('#z2').click(function(){
            v = ++counter;
            return v;
        })
    } // end of  var test
    
    var p = test();
    

    【讨论】:

    • 感谢您发现我的错误。是的,该函数返回未定义。但我没有传递全局变量 v.which 在里面是可见的。我想在外面看到 v 的值。请问我怎样才能得到它。
    【解决方案3】:

    您的代码有两个主要问题。

    如果你运行它,它会抛出一个错误(1): test.testdata is not a function ! 如果您使用控制台查找 test.testdata,您会看到它是 undefined (error(2))。 如果您尝试 var p = testdata(),它也会抛出错误 (3): testdata is undefined !

    您已在 inside 函数测试中定义了函数 testdata。测试中的任何代码都可以看到 您的函数 testdata,但任何代码 outside 测试都看不到它。 您调用 testdata 的 var p 在外部。这就是错误(3)的原因。

    通过在 test 中定义 testdata,您可能认为它会自动添加到 test 作为属性。事实并非如此。您可以向函数添加属性,但这必须在该函数之外进行。这就是错误(1)和(2)的原因。

    让我们试试吧:

    // declare your functions by name,
    // you later can call them by name without need for a variable
    function test() {
        // one variable is enough here
        var c = 0;
        $("#z2").click(function() {
            ++c;
            testdata();
        });
    }
    // function testdata declared by name outside function test
    function testdata() {return c;}
    // now we can attach testdata as a property to test
    test.testdata = testdata;
    
    var p = c; // should be 0 but is undefined
    var p = testdata(); // no error, but undefined.
    var p = test.testdata(); // no error, but undefined. Why?
    

    与 error(3) 相同的问题,但现在使用变量 c。它被声明为 inside 测试, 外面的所有代码都看不到。 (查找关键字 scopeclosure 以了解更多信息);

    我们再试一次:

    // declare var c outside, so it's in the global scope,
    //all following code can see it.
    var c = 0;
    function test() {
        $("#z2").click(function() {
            ++c;
            testdata();
        });
    }
    function testdata() {return c;}
    test.testdata = testdata;
    
    var p = c; // --> 0
    var p = testdata(); // --> 0
    var p = test.testdata(); // --> 0
    

    好的,现在开始使用您的点击功能。

    【讨论】:

    • 我的用户名是 Martin Ernst,三天以来是新用户。我在回答之前忘记登录,所以似乎我创建了一个新用户。有人知道如何解决这个问题吗?非常感谢。
    • @Martin Ernst:我找到了将用户合并在一起的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多