Mootools Class to Jquery
Mootools Class to Jquery
我正在将mootools类转换为Jquery,但目前我遇到了一个问题。
我有以下代码(mootools)var ListaItens = new Class({
...
...
initialize: function(element){
....
this.btnAdd = this.tabela.getElement('.add_linha button');
if(this.btnAdd){
this.btnAdd.addEvent('click', this.addLinha.bindWithEvent(this));
this.checkAdd();
}
...
},
addLinha: function(){
}
});
现在在Jquery中我有这个
var ListaItens = function(element, maxLinhas){
...
this.btnAdd = this.tabela.find('.add_linha button')[0];
if(this.btnAdd){
//$(this.btnAdd).proxy('click', this.addLinha);
$(this.btnAdd).on("click", this.addLinha);
this.checkAdd;
}
...
this.addLinha = function(){
}
})
我的问题是如何绑定addline函数btnAdd。我的代码不工作,因为元素"this"的变化。我不知道如何将bindWithEvent函数转换为jquery。
解决方案吗?
Thanks in advance
据我所知,jQuery不提供与mootools类似的类概念,因此您可以使用"经典JS方法"来创建类,如下所示:
function ListaItens () {
var that = this;
this.tabela = $(…);
this.btnAdd = this.tabela.find(…);
if (this.btnAdd) {
this.this.btnAdd.on('click', function (e) {
that.addLinha();
})
}
}
ListaItens.prototype.addLinha = function () {
}
var instance = new ListaItens();
那么,要回答你的问题:你基本上需要做的就是保持对"原始this "的引用
相关文章:
- grep in JQuery to C#
- jQuery to pass Checkbox Update mysql
- jQuery to Javascript Conversion for AJAX request
- jQuery to shift object
- ajax post with jquery to mongodb
- JQuery to regular JavaScript
- JQuery to css write nth-child()
- jquery to javascript translation
- Javascript/JQuery to do rollover-effect confirm
- MVC4 jquery to deselect Html.CheckboxListFor
- jQuery to regular Javascript - overlay
- jQuery to plain javascript
- JQuery to Fixed()未正确显示
- Click Listener from jQuery to Javascript
- Adding JavaScript JQuery to HTA
- 可以't find why this js/jQuery to list last 12 months gene
- Turn '$(this)' jquery to javascript 'this'
- Jquery to Mootools
- Javascript/Jquery to change class onclick?
- javascript/jquery to change location.href