Javascript detachEvent,它附加了$.proxy函数
我试图在一个表单集中创建一个Django表单,当最后一个表单得到一些输入时,它会自动在它的末尾添加一个新表单。我在这个js中使用输入检测: 我这里的功能在Firefox中运行良好,我假设任何其他支持addEventListener和removeEventListener的浏览器都可以。我不明白如何正确地将detachEvent设置为IE后备?我对javascript不是很了解,只是想把东西拼凑起来Javascript detachEvent,它附加了$.proxy函数,javascript,internet-explorer,Javascript,Internet Explorer,我试图在一个表单集中创建一个Django表单,当最后一个表单得到一些输入时,它会自动在它的末尾添加一个新表单。我在这个js中使用输入检测: 我这里的功能在Firefox中运行良好,我假设任何其他支持addEventListener和removeEventListener的浏览器都可以。我不明白如何正确地将detachEvent设置为IE后备?我对javascript不是很了解,只是想把东西拼凑起来 jQuery.fn.cloneOnInput = function(prefix){ r
jQuery.fn.cloneOnInput = function(prefix){
return this.each(function() {
var trElement = this
var inputElements = $(this).find('input')
var cloneForm = function() {
cloneMore(trElement, prefix);
inputElements.each(function(index) {
if ("onpropertychange" in this) {
// What here?
}
else {
this.removeEventListener("input", cloneForm);
}
});
};
inputElements.each(function(index) {
// Do I have to change the way it attaches?
if ("onpropertychange" in this) {
this.attachEvent($.proxy(function () {
if (event.propertyName == "value")
cloneForm();
}, this));}
else {
this.addEventListener("input", cloneForm, false);
}
});
});
};
您必须跟踪代理处理程序,以便以后删除它。由于处理程序与DOM元素关联,因此可以使用来实现这一点:
jQuery.fn.cloneOnInput = function(prefix) {
return this.each(function() {
var trElement = this;
var inputElements = $(this).find("input");
var cloneForm = function() {
cloneMore(trElement, prefix);
inputElements.each(function() {
if ("onpropertychange" in this) {
this.detachEvent("onpropertychange",
$(this).data("proxiedHandler"));
$(this).removeData("proxiedHandler");
} else {
this.removeEventListener("input", cloneForm);
}
});
};
inputElements.each(function(index) {
// Do I have to change the way it attaches?
if ("onpropertychange" in this) {
var proxiedHandler = $.proxy(function() {
if (event.propertyName == "value") {
cloneForm();
}
}, this);
$(this).data("proxiedHandler", proxiedHandler);
this.attachEvent("onpropertychange", proxiedHandler);
} else {
this.addEventListener("input", cloneForm, false);
}
});
});
};
如果您已经在使用jQuery Cheers,那么应该使用jQuery事件系统,非常感谢。但我不知道jQuery有事件处理,我想我也会调查一下。只学习完成某件事情所需要的东西的陷阱是,在这个过程中,我错过了更多有用的东西。