2

我有一个布局这样的结构ASP.NET MVC 3 + jQuery Mobile的应用:ASP.NET MVC + jQuery Mobile的事件处理程序

<body> 
    <div class="page" data-role="page" data-add-back-btn="true" id="page"> 
     <div data-role="header" data-position="fixed"></div> 
     <div data-role="content" id="content"> 
      @RenderBody() 
     </div> 
     <div id="footer" data-role="footer" data-position="fixed"></div> 
    </div> 
</body> 

的问题是,绑定到卡住几页窗事件处理程序。

例如我有2页:"Index""About"。在"Index"我结合一些处理(比如console.log("index"))$(window).click()事件但是,当我去"About"页面 - 这个处理器仍然活跃

有什么办法让处理程序只有在相应的页面是活跃

回答

0

我对这个问题做了小小的研究,但没有找到任何合适的问题。 因此,我已经为窗口事件实现了描述用例的解决方案。这是令人毛骨悚然,但工程。

布局:

1.Page DIV声明:

<div class="page" data-role="page" data-add-back-btn="true" id="@Request.RawUrl.GetHashCode()"> 
    ... 
</div> 

2.Scripts:

<script type="text/javascript"> 
    var bindEvents = []; 
    $(document).bind('pagehide', function (event) { 
     var hash = $(event.target).attr('id'); 
     $(window).unbind('.' + hash); 
    }); 

    $(document).bind('pageshow', function (event) { 
     var hash = $(event.target).attr('id'); 
     bindEvents[hash](); 
    }); 
</script> 

在网页:

1.Index:

<script type="text/javascript"> 
var hashedIndex = '@Request.RawUrl.GetHashCode()'; 
if (!bindEvents.hasOwnProperty(hashedIndex)) { 
    bindEvents[hashedIndex] = function() { 
     $(window).bind('click.' + hashedIndex, function() { 
      console.log('index'); 
     }); 
    }; 
}; 
</script> 

2.关于:

<script type="text/javascript"> 
var hashedAbout = '@Request.RawUrl.GetHashCode()'; 
if (!bindEvents.hasOwnProperty(hashedAbout)){ 
    bindEvents[hashedAbout] = function() { 
     $(window).bind('click.' + hashedAbout, function() { 
      console.log('about'); 
     }); 
    }; 
}; 
</script> 

,如果需要,类似的其他网页。

在一般情况下,我同意Gajotres:最好将事件绑定到某些内部容器以避免此类问题。

1

使用?这类事件与JQM结合:

$('#page').bind('click', function(e) { 

}); 

使用jQuery的较新版本的使用。对(和.off(绑定/解除绑定事件$( '#页')是你的页面

钍。是:

$(window).click() 

将它绑定到窗口,因为jQM页面是单个窗口事件,每次都会触发。 你还需要担心多个事件绑定,here你可以找到更多关于这个问题的信息。

+0

不幸的是,我需要处理确切的窗口事件(滚动),所以绑定到一些内部容器是不可能在我的应用程序的上下文中。 –

+0

然后你需要找到一个方法,jQM web应用程序是一个单一的窗口应用程序。你用窗口事件(滚动)做什么? – Gajotres

+0

无尽的滚动。我试过基于iscroll的插件,但它们在某些平台上有点bug。现在我正在寻找无需使用窗口对象来实现无限滚动的方式 - 希望它能帮助解决这个问题。 –