Javascript jQuery on(";click";)在$(窗口)之后不工作。on(&x27;hashchange&&x27;)并清空主div

Javascript jQuery on(";click";)在$(窗口)之后不工作。on(&x27;hashchange&&x27;)并清空主div,javascript,jquery,cordova,handlebars.js,Javascript,Jquery,Cordova,Handlebars.js,我正在编写一个带有主页和设置页的cordova应用程序。它必须是一个单页应用程序,以便通过更改url哈希来更改正文内容,我在('hashchange')上使用$(window)和“a”标记,在主页上使用href=“#setting”,在设置页面上使用href=“#”来更改内容 主页上有一个按钮,设置上有一个按钮,它们都有一个点击jQuery事件 当应用程序初始化时,主页上的按钮工作正常。但是,如果我单击href=“#设置”链接,设置页面上的按钮不会做任何事情,尽管它有一个点击事件。(返回主页,主

我正在编写一个带有主页和设置页的cordova应用程序。它必须是一个单页应用程序,以便通过更改url哈希来更改正文内容,我在('hashchange')上使用$(window)和“a”标记,在主页上使用href=“#setting”,在设置页面上使用href=“#”来更改内容

主页上有一个按钮,设置上有一个按钮,它们都有一个点击
jQuery事件

当应用程序初始化时,主页上的按钮工作正常。但是,如果我单击href=“#设置”链接,设置页面上的按钮不会做任何事情,尽管它有一个点击事件。(返回主页,主页按钮也不再工作)

更新:每次哈希更改时,#main div都会删除,并创建一个新的#main div。这是原因吗?改变dom结构?如果是,我如何修复它?我的视图是在HomeView对象中生成的

代码如下:

var-app={
初始化:函数(){
document.addEventListener('devicerady',this.ondevicerady.bind(this),false);
},
ondevicerady:function(){
这条路线();
this.url=/^#settings$/;
this.receivedEvent();
},
receivedEvent:function(){
var self=这个;
$('#btnSave')。在('click',函数(){
self.save();
});
$('btnLocate')。在('click',函数(){
self.calc();
});
$(window.on('hashchange',this.route.bind(this));
}
,
播放:功能(pos){
//做点什么
},
路由:函数(){
var hash=window.location.hash;
if(!hash){
如有需要(本网页){
this.showPage(this.homePage);
}否则{
this.homePage=new HomeView().render();
this.showPage(this.homePage);
}
//this.receivedEvent();
返回;
}
var self=这个;
if(hash.match(this.url)){
如果(本设置页){
this.showPage(this.settingPage);
}否则{
this.settingPage=new SettingView(self.crl.render();
this.showPage(this.settingPage);
}
//this.receivedEvent();
}
},
显示页面:功能(第页){
如果($('#main')){
$('#main')。删除();
}
$('body').append(page.el);
},
计算:函数(){
//做点什么
},
保存:函数(){
//做点什么
}
};

app.initialize()我认为您需要修改代码,如下所示:

onDeviceReady: function() {
    this.route();
    this.url= /^#settings$/ ;
    this.receivedEvent();

    var self= this;
    //using event delegation technique to attach event listner
    $(document).on('click','#btnSave', function () {
        self.save();
    });
    $(document).on('click','#btnLocate', function () {
        self.calc();
    });

    $(window).on('hashchange', this.route.bind(this));

},
1)
ondevicerady:
应具有jQuery事件处理程序初始化代码,如下所示:

onDeviceReady: function() {
    this.route();
    this.url= /^#settings$/ ;
    this.receivedEvent();

    var self= this;
    //using event delegation technique to attach event listner
    $(document).on('click','#btnSave', function () {
        self.save();
    });
    $(document).on('click','#btnLocate', function () {
        self.calc();
    });

    $(window).on('hashchange', this.route.bind(this));

},
上述代码将只执行一次

2)
receivedEvent:
不应具有jQuery事件处理程序附件代码:

receivedEvent: function() {

},
3) 注释
//this.receivedEvent()行应保持原样


我希望这能解决您的问题。

谢谢!但这一次连主页上的按钮都不起作用,我想这是因为cordova应用程序必须等到设备就绪事件。我在cordova服务器上运行该页面。请您将事件侦听器附件代码移到上面的
ondevicerady:
,很抱歉。刚才将侦听器移动到了onDeviceReady,与第一个相同。对不起,我的英语不好。你有什么错误吗。您可以在浏览器的控制台中检查它们。不,没有错误。我使用console.log通过单击按钮来确保可能发生某些事情,但我在控制台中什么也没有得到。