Javascript 如何解除绑定或关闭所有jquery函数?
我已经构建了一个具有推送状态的应用程序。一切正常。然而,在某些情况下,我的jquery函数会多次启动。这是因为当我调用push state时,我会为我调用的每个页面绑定特定的js文件。这意味着,当我在我的页面上冲浪时,相同的js函数会多次绑定到html 提示:我在jquery函数中使用documen.on,因为我需要函数通过Ajax绑定到动态打印的HTML 在打印之前,我尝试在推送状态下使用off,但没有成功 这是我的密码:Javascript 如何解除绑定或关闭所有jquery函数?,javascript,jquery,ajax,pushstate,Javascript,Jquery,Ajax,Pushstate,我已经构建了一个具有推送状态的应用程序。一切正常。然而,在某些情况下,我的jquery函数会多次启动。这是因为当我调用push state时,我会为我调用的每个页面绑定特定的js文件。这意味着,当我在我的页面上冲浪时,相同的js函数会多次绑定到html 提示:我在jquery函数中使用documen.on,因为我需要函数通过Ajax绑定到动态打印的HTML 在打印之前,我尝试在推送状态下使用off,但没有成功 这是我的密码: var requests = []; functio
var requests = [];
function replacePage(url) {
var loading = '<div class="push-load"></div>'
$('.content').fadeOut(200);
$('.container').append(loading);
$.each( requests, function( i, v ){
v.abort();
});
requests.push( $.ajax({
type: "GET",
url: url,
dataType: "html",
success: function(data){
var dom = $(data);
//var title = dom.filter('title').text();
var html = dom.find('.content').html();
//alert(html);
//alert("OK");
//$('title').text(title);
$('a').off();
$('.push-load').remove();
$('.content').html(html).fadeIn(200);
//console.log(data);
$('.page-loader').hide();
$('.load-a').fadeIn(300);
}
})
);
}
$(window).bind('popstate', function(){
replacePage(location.pathname);
});
var请求=[];
函数替换页(url){
变量加载=“”
$('.content')。淡出(200);
$('.container')。追加(加载);
$。每个(请求、功能(i、v){
v、 中止();
});
requests.push($.ajax({
键入:“获取”,
url:url,
数据类型:“html”,
成功:函数(数据){
var dom=$(数据);
//var title=dom.filter('title').text();
var html=dom.find('.content').html();
//警报(html);
//警报(“正常”);
//$('title')。文本(title);
$('a').off();
$('.push-load').remove();
$('.content').html(html).fadeIn(200);
//控制台日志(数据);
$('.page loader').hide();
$('.load-a').fadeIn(300);
}
})
);
}
$(窗口).bind('popstate',function(){
replacePage(location.pathname);
});
提前谢谢 使用空代码简单绑定新函数
$( "#id" ).bind( "click", function() {
//blank
});
或
使用
试试这个
var requests = [];
function replacePage(url) {
var obj = $(this);
obj.unbind("click", replacePage); //unbind to prevent ajax multiple request
var loading = '<div class="push-load"></div>';
$('.content').fadeOut(200);
$('.container').append(loading);
$.each(requests, function (i, v) {
v.abort();
});
requests.push(
$.ajax({
type: "GET",
url: url,
dataType: "html",
success: function (data) {
var dom = $(data);
//var title = dom.filter('title').text();
var html = dom.find('.content').html();
//alert(html);
//alert("OK");
//$('title').text(title);
obj.bind("click", replacePage); // binding after successfulurl ajax request
$('.push-load').remove();
$('.content').html(html).fadeIn(200);
//console.log(data);
$('.page-loader').hide();
$('.load-a').fadeIn(300);
}
}));
}
var请求=[];
函数替换页(url){
var obj=$(本);
取消绑定(“单击”,替换页面);//取消绑定以防止ajax多个请求
var加载=“”;
$('.content')。淡出(200);
$('.container')。追加(加载);
$。每个(请求、功能(i、v){
v、 中止();
});
请求推送(
$.ajax({
键入:“获取”,
url:url,
数据类型:“html”,
成功:功能(数据){
var dom=$(数据);
//var title=dom.filter('title').text();
var html=dom.find('.content').html();
//警报(html);
//警报(“正常”);
//$('title')。文本(title);
bind(“单击”,替换页面);//成功URL ajax请求后的绑定
$('.push-load').remove();
$('.content').html(html).fadeIn(200);
//控制台日志(数据);
$('.page loader').hide();
$('.load-a').fadeIn(300);
}
}));
}
希望这有帮助,谢谢你这不是一个理想的方式$(“#id”).unbind();另一种方法你可以使用为什么不通过添加某种标志来检查你是否已经绑定了一个事件?我知道我绑定了一次事件。但我想在每次调用此函数时解除绑定并绑定事件。您绑定的是什么事件。@DKM我使用的是.off(),如果您可以看到但没有效果的话!
var requests = [];
function replacePage(url) {
var obj = $(this);
obj.unbind("click", replacePage); //unbind to prevent ajax multiple request
var loading = '<div class="push-load"></div>';
$('.content').fadeOut(200);
$('.container').append(loading);
$.each(requests, function (i, v) {
v.abort();
});
requests.push(
$.ajax({
type: "GET",
url: url,
dataType: "html",
success: function (data) {
var dom = $(data);
//var title = dom.filter('title').text();
var html = dom.find('.content').html();
//alert(html);
//alert("OK");
//$('title').text(title);
obj.bind("click", replacePage); // binding after successfulurl ajax request
$('.push-load').remove();
$('.content').html(html).fadeIn(200);
//console.log(data);
$('.page-loader').hide();
$('.load-a').fadeIn(300);
}
}));
}