ASP.NET MVC+;JQuery移动事件处理程序
我有ASP.NET MVC 3+JQuery移动应用程序,布局结构如下:ASP.NET MVC+;JQuery移动事件处理程序,jquery,asp.net,asp.net-mvc-3,jquery-mobile,jquery-events,Jquery,Asp.net,Asp.net Mvc 3,Jquery Mobile,Jquery Events,我有ASP.NET MVC 3+JQuery移动应用程序,布局结构如下: <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="conten
<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>
@RenderBody()
问题是,绑定到窗口的事件处理程序会在几个页面中卡住
例如,我有两个页面:“索引”
和“关于”
。在“Index”
中,我在$(窗口)上绑定了一些处理程序(比如console.log(“Index”)
。单击()。但当我转到“关于”
页面时,该处理程序仍然处于活动状态
有没有办法只在适当的页面处于活动状态时保留处理程序?使用jQM的这种事件绑定:
$('#page').bind('click', function(e) {
});
对于较新版本的jQuery,使用.on(和.off(绑定/取消绑定事件)。$(“#page”)是您的页面
这:
将它绑定到窗口,因为jQM页面是一个窗口,每次都会触发事件。
您还需要担心多事件绑定,您可以找到有关此问题的更多信息。我对此问题做了一些小研究,但没有找到任何合适的此类问题。 所以我已经为描述的窗口事件用例实现了解决方案。这很令人毛骨悚然,但很有效 在布局中: 1.第页分区声明:
<div class="page" data-role="page" data-add-back-btn="true" id="@Request.RawUrl.GetHashCode()">
...
</div>
...
2.脚本:
<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>
var bindEvents=[];
$(文档).bind('pagehide',函数(事件){
var hash=$(event.target).attr('id');
$(窗口).unbind('.'+散列);
});
$(文档).bind('pageshow',函数(事件){
var hash=$(event.target).attr('id');
bindEvents[hash]();
});
第页:
1.索引:
<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>
var hashedIndex='@Request.RawUrl.GetHashCode()';
如果(!bindEvents.hasOwnProperty(hashedIndex)){
bindEvents[hashedIndex]=函数(){
$(窗口).bind('click.'+hashedIndex,function(){
console.log('index');
});
};
};
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>
var hashedAbout='@Request.RawUrl.GetHashCode()';
如果(!bindEvents.hasOwnProperty(hashedAbout)){
bindEvents[hashedAbout]=函数(){
$(窗口).bind('click.'+hashedAbout,function(){
console.log('about');
});
};
};
如果需要,也可以与其他页面类似
一般情况下,我同意Gajotres:最好将事件绑定到一些内部容器以避免此类问题。不幸的是,我需要准确处理窗口事件(滚动),因此在我的应用程序上下文中无法绑定到某个内部容器。然后,您需要找到一种解决方法,jQM web app是一个单一窗口应用程序。您如何处理窗口事件(滚动)?无休止的滚动。我尝试过基于iscroll的插件,但在某些平台上有点缺陷。现在我正在寻找不使用窗口对象实现无休止滚动的方法-希望它能帮助解决这个问题。Berker Yüceer:它是在索引中定义的。
<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>