以下代码跟踪用户遍历表单完成了多少次点击或选项卡,并添加了行为分数:
$(function() {
$.fn.clickCount = function() {
var clickCount = 0;
var clickBehaviour = 0;
return {
increment: function() {
clickCount++;
},
behaviour: function() {
clickBehaviour -= 5;
},
print: function() {
console.log('Click count:' + clickCount);
console.log('Click behaviour:' + clickBehaviour);
}
};
};
$.fn.tabCount = function() {
var tabCount = 0;
var tabBehaviour = 0;
return {
increment: function() {
tabCount++;
},
behaviour: function() {
tabBehaviour += 5;
},
print: function() {
console.log('Tab count:' + tabCount);
console.log('Tab behaviour:' + tabBehaviour);
}
};
};
var $input = $('input, select, textarea');
var c = $.fn.clickCount();
var t = $.fn.tabCount();
$input.mousedown(function() {
c.increment();
c.behaviour();
c.print();
});
$input.keydown(function(e) {
var keyCode = e.keyCode || e.which;
if (e.keyCode === 9) {
$(this).each(function() {
t.increment();
t.behaviour();
t.print();
});
};
});
});
我现在希望能够将 和 的值相加,clickBehaviour
并tabBehaviour
在每次单击时将其输出到控制台或tab
我已经尝试过了,但由于我对 JavaScript 的了解有限,我不断返回NaN