Javascript detachEvent,它附加了一个 $.proxy 函数
Javascript detachEvent, which was attached with a $.proxy function
我正在尝试在表单集中制作一个 Django 表单,当最后一个表单获得一些输入时,它会自动在其末尾添加一个新表单。我正在此js中使用输入检测:http://whattheheadsaid.com/2010/09/effectively-detecting-user-input-in-javascript
我在这里拥有的东西在Firefox中运行良好,我假设任何其他支持addEventListener和removeEventListener的浏览器。我不明白如何正确设置 detachEvent 作为 IE 回退?我对javascript并不是那么了解,只是试图把东西拼凑在一起。
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 元素相关联,因此您可以使用 data() 来实现这一点:
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);
}
});
});
};
相关文章:
- 我可以在json对象中添加一个函数吗
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在另一个函数中使用变量this
- 在另一个函数成功结束后调用该函数
- mongoose.connect undefined不是一个函数
- 监听器必须是一个函数
- 使用JS函数来使用另一个函数的语法?node.js
- 如何取消object.prototypes javascript的一个函数
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 嵌套到另一个函数中的Fancybox函数;不起作用
- 如何在javascript中使用不止一个函数
- jQuery-在页面加载时执行一个函数
- jquery UI draggable:UI.children不是一个函数
- jQuery Mobile Undefined不是一个函数
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- javascript函数将数据添加到屏幕,但随后被另一个函数覆盖
- JS异常:animate不是一个函数
- 如何将一个函数附加到一个不存在的元素上
- JavaScript/jQuery-添加添加和删除类与下一个函数之间的延迟