包含 javascript 文件后,我如何 delete/remove
After include javascript file, how i can delete/remove
因为我使用 ajax 来访问我网站上的内容;我有此代码 # 1,它动态包含不同目录的 javascript 文件(取决于加载的内容),但包含相同的功能。
代码#1:
var formContent = $("body form").first().attr('name');
if (typeof formContent != 'undefined') {
var uri = "classJS/" + formContent + "/jsc.js"; //File Javascript repeate in diferent directories.
fetch(uri, { method: 'GET', type: 'HEAD' }).then(function(resp) {
if (resp['status'] === 200) {
//if Exist the Default Function (FunctionForms) set this to undefined.
if (window.isFunction(window.FunctionForms)) {
window.FunctionForms = undefined;
}
$.getScript(uri).done(function() {
window.FunctionForms(formEvent); //call or Re-Call the function.
});
} else {
console.log('%cNot find Script file: ' + formEvent, 'color: red');
}
});
}
代码#2,此功能(在每个文件中重复)专用于为加载的内容收取其他特定功能,示例:
代码#2 文件1:(可加载N次)
function FunctionForms(formEvent) {
console.log('%cCarga de Script JS para: ' + formEvent, 'background: #222; color: #bada55');
window.Test();
}
function Test(){
$('[name="s_list_2"]').unbind('click.form');
$(document).on("click.form", '[name="s_list_2"]', function(event) {
console.log('Is clicked');
});
}
来自其他目录的同一个文件可能有细微的不同内容:
function FunctionForms(formEvent) {
//not used.........
console.log('%cCarga de Script JS para: ' + formEvent, 'background: #222; color: #bada55');
}
然后:如果我输入相同的内容5次;系统包含5次相同的文件;但它不会覆盖函数或删除它,相反,它会将它存储在不同的实例中;导致它 运行 N 次:在控制台输出中我得到这个。
VM6933:27 Is clicked
VM6944:27 Is clicked
VM6986:27 Is clicked
VM7022:27 Is clicked
VM7105:27 Is clicked
我明白这显然不是我所期望的:
if (window.isFunction(window.FunctionForms)) {
window.FunctionForms = undefined;
}
问题不在于函数,而在于事件绑定。每次加载文件时,它都会:
$(document).on("click.form", '[name="s_list_2"]', function(event) {
console.log('Is clicked');
});
它添加了另一个点击处理程序。
您似乎尝试先删除旧的处理程序,但操作不正确。要删除委托的事件处理程序,您必须使用与 .off()
:
相同的委托语法
$(document).off("click.form", '[name="s_list_2"]');
您正在使用:
$('[name="s_list_2"]').unbind('click.form');
这只会删除已添加的处理程序:
$('[name="s_list_2"]').on('click.form', function ...);
因为我使用 ajax 来访问我网站上的内容;我有此代码 # 1,它动态包含不同目录的 javascript 文件(取决于加载的内容),但包含相同的功能。
代码#1:
var formContent = $("body form").first().attr('name');
if (typeof formContent != 'undefined') {
var uri = "classJS/" + formContent + "/jsc.js"; //File Javascript repeate in diferent directories.
fetch(uri, { method: 'GET', type: 'HEAD' }).then(function(resp) {
if (resp['status'] === 200) {
//if Exist the Default Function (FunctionForms) set this to undefined.
if (window.isFunction(window.FunctionForms)) {
window.FunctionForms = undefined;
}
$.getScript(uri).done(function() {
window.FunctionForms(formEvent); //call or Re-Call the function.
});
} else {
console.log('%cNot find Script file: ' + formEvent, 'color: red');
}
});
}
代码#2,此功能(在每个文件中重复)专用于为加载的内容收取其他特定功能,示例:
代码#2 文件1:(可加载N次)
function FunctionForms(formEvent) {
console.log('%cCarga de Script JS para: ' + formEvent, 'background: #222; color: #bada55');
window.Test();
}
function Test(){
$('[name="s_list_2"]').unbind('click.form');
$(document).on("click.form", '[name="s_list_2"]', function(event) {
console.log('Is clicked');
});
}
来自其他目录的同一个文件可能有细微的不同内容:
function FunctionForms(formEvent) {
//not used.........
console.log('%cCarga de Script JS para: ' + formEvent, 'background: #222; color: #bada55');
}
然后:如果我输入相同的内容5次;系统包含5次相同的文件;但它不会覆盖函数或删除它,相反,它会将它存储在不同的实例中;导致它 运行 N 次:在控制台输出中我得到这个。
VM6933:27 Is clicked
VM6944:27 Is clicked
VM6986:27 Is clicked
VM7022:27 Is clicked
VM7105:27 Is clicked
我明白这显然不是我所期望的:
if (window.isFunction(window.FunctionForms)) {
window.FunctionForms = undefined;
}
问题不在于函数,而在于事件绑定。每次加载文件时,它都会:
$(document).on("click.form", '[name="s_list_2"]', function(event) {
console.log('Is clicked');
});
它添加了另一个点击处理程序。
您似乎尝试先删除旧的处理程序,但操作不正确。要删除委托的事件处理程序,您必须使用与 .off()
:
$(document).off("click.form", '[name="s_list_2"]');
您正在使用:
$('[name="s_list_2"]').unbind('click.form');
这只会删除已添加的处理程序:
$('[name="s_list_2"]').on('click.form', function ...);