[英]JQuery Events not Firing after re-rendering Page?
I am working on a Backbone application which is based on a dynamic template . 我正在开发基于动态模板的Backbone应用程序。 I have a header view, a side panel view and footer view that are dynamically initialized when calling any other view. 我有一个标题视图,一个侧面板视图和页脚视图,这些视图在调用任何其他视图时会动态初始化。 I am using i18n to have the possibility to switch languages. 我正在使用i18n来切换语言。 The Problem is, when I choose a different language and when I call the render function the JQuery events don't fire any more. 问题是,当我选择其他语言并调用render函数时,JQuery事件不再触发。 The template is rendered successfully but the buttons doesn't work. 模板已成功呈现,但按钮不起作用。
My Backbone View : 我的骨干网视图:
define([ "jquery", "backbone", "text!../../pages/header.html" ], function($,
Backbone, headerTpl) {
var header = Backbone.View.extend({
initialize : function(options) {
this.render();
},
template : _.template(headerTpl),
events : {
"click #enBtn" : "swichToEnglish",
"click #frBtn" : "swichToFrench"
},
render : function() {
this.$el.html(this.template());
this.delegateEvents();
return this;
},
swichToFrench : function() {//Switching to French
if (i18n.currentLocal == 'en') {
i18n.currentLocal = 'fr';
this.$el.find("#frBtn").css("pointer-events", "auto");
this.render();//Re-rendering the template
}
},
swichToEnglish : function() {//Switching to English
if (i18n.currentLocal == 'fr') {
i18n.currentLocal = 'en';
this.$el.find("#enBtn").css("pointer-events", "auto");
this.$el.find("#frBtn").css("pointer-events", "none");
this.render();//Re-rendering the template
}
}
});
return header;
});
The events declared in the events object works but I have external events, JQuery events that are declared in an external file . 在events对象中声明的事件有效,但是我有外部事件,即在外部文件中声明的JQuery事件。
Example of a Jquery event: jQuery事件的示例:
$('#menu_toggle').on('click',function () {
if ($('body').hasClass('nav-md')) {
$('body').removeClass('nav-md');
$('body').addClass('nav-sm');
$('.left_col').removeClass('scroll-view');
$('.left_col').removeAttr('style');
$('.sidebar-footer').hide();
if ($('#sidebar-menu li').hasClass('active')) {
$('#sidebar-menu li.active').addClass('active-sm');
$('#sidebar-menu li.active').removeClass('active');
}
} else {
$('body').removeClass('nav-sm');
$('body').addClass('nav-md');
$('.sidebar-footer').show();
if ($('#sidebar-menu li').hasClass('active-sm')) {
$('#sidebar-menu li.active-sm').addClass('active');
$('#sidebar-menu li.active-sm').removeClass('active-sm');
}
}
});
Any Ideas how to make the JQuery events fire after re-rendering my template? 有任何想法如何使JQuery事件在重新渲染模板后触发? Knowing that All my Jquery events are defined in an external file called custom.js that I call when I initialize my App (Require Js) Thank You. 知道我所有的Jquery事件都定义在一个名为custom.js的外部文件中,在初始化我的应用程序时会调用该文件(Require Js)。谢谢。
If the DOM changes, your will loose your event binds too.. 如果DOM更改,您的事件绑定也将丢失。
Try with this instead: 试试这个代替:
$(document).on('click', '#menu_toggle', function () {
//...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.