Jquery .click()带有unanonymus方法问题

时间:2015-12-27 17:19:55

标签: javascript jquery

我正在关注并修改教程,希望创建一个交互式技能树表

var main = function() {
    var points = parseInt($('.resume').text());
    var pointExpended = 0; 


    $('.node').click(function() {
        var cost = parseInt($(this).children('.info').children('.cost').text());
        if($(this).hasClass('active-node')) {
            //l'abilità viene rimossa 
            $(this).removeClass('active-node');
            pointExpended = pointExpended - cost;
            $('.resume').text(points-pointExpended);
        }
        else {
            //l'abilità viene comprata
            if(points-(pointExpended+cost) < 0) {
                alert("Limite dei punti superato");
            }
            else {
            $(this).addClass('active-node');
            pointExpended = pointExpended + cost;
            $('.resume').text(points-pointExpended);
            }
        }
    });
   };


   $(document).ready(main);

添加一些递归我试图将处理程序放入外部函数

var click_handler = function() {

        alert(points);

        var cost = parseInt($(this).children('.info').children('.cost').text());
        if($(this).hasClass('active-node')) {
            //l'abilità viene rimossa 
            $(this).removeClass('active-node');
            pointExpended = pointExpended - cost;
            $('.resume').text(points-pointExpended);
        }
        else {
            //l'abilità viene comprata
            if(points-(pointExpended+cost) < 0) {
                alert("Limite dei punti superato");
            }
            else {
            $(this).addClass('active-node');
            pointExpended = pointExpended + cost;
            $('.resume').text(points-pointExpended);
            }
        }
    }

var main = function() {
    var points = parseInt($('.resume').text());
    var pointExpended = 0; 

    alert(points);

    $('.node').click(click_handler);
};


$(document).ready(main); 

但代码不响应点击次数。

我对jscript / jquery很新,所以我无法理解为什么第一次尝试有效而不是后者

1 个答案:

答案 0 :(得分:2)

变量public class myListAdapter extends ArrayAdapter<Person> { public myListAdapter() { super(SelectContactsActivity.this, R.layout.da_item, list); } @Override public View getView(int position, View convertView, ViewGroup parent) { View itemView = convertView; if (itemView == null) { itemView = getLayoutInflater().inflate(R.layout.da_item, parent, false); } // Find person wot work with Person currentperson = list.get(position); // Fill the view TextView nameboxview = (TextView) itemView.findViewById(R.id.NameView); nameboxview.setText(currentperson.getName()); TextView numberboxview = (TextView) itemView.findViewById(R.id.NumberView); numberboxview.setText(currentperson.getPhone()); CheckBox cb = (CheckBox)itemView.findViewById(R.id.checkBox); cb.setTag(position); if (cb.isChecked()) { currentperson.setSelected(true); } return itemView; } } points对您的点击处理程序不可见(变量作用域是词法的东西,并且在调用堆栈上不起作用..)因此您需要将它们传递给封闭:

pointEtended

还修改click_handler以接受命名值

var main = function() {
    var points = parseInt($('.resume').text());
    var pointExpended = 0; 

    alert(points);

    $('.node').click(function(evt) {
              click_handler(evt, points, pointExtended)
    });
};