背景不取消链接方向
我一直在试图取消链接方向时,点击事件在jQuery Mobile的应用定位元素上触发这个愚蠢的小问题所困扰。event.preventDefault()在jQuery Mobile的
比方说,我有一个简单的多页文档是这样的:
<div data-role="page" id="page-1">
<div data-role="content">
<a href="#page-2" id="mLink">page 2</a>
</div><!-- /content -->
</div><!-- /page -->
<div data-role="page" id="page-2">
<div data-role="content">
</div><!-- /content -->
</div><!-- /page -->
...和JavaScript这样的:
(function (MyApp, $, undefined) {
'use strict';
// Initializes app
function init() {
$('#mLink').on('click', function (event) {
event.preventDefault();
//event.stopPropagation();
//event.stopImmediatePropagation();
});
}
// jQuery Mobile is ready now -> override defaults
$(document).on("mobileinit", function() {
// Set the default page transition
$.mobile.defaultPageTransition = 'slide';
});
// jQuery Mobile is ready now
$(document).ready(init);
}(window.MyApp = window.MyApp || {}, jQuery));
问题
我只是想不通为什么event.preventDefault()
没有取消页面转换。但是,如果我添加event.stopPropagation()
它将取消它。另外,如果我从应用程序中删除jQuery Mobile库,它的工作原理没有event.stopPropagation()
。
问题
这是正常的行为,它是确定总是叫他们无论是在处理程序,取消路段方向?
注意
我使用jQuery Mobile的只为它的多页模板,导航和过渡能力。
其他问题
问题的背后潜伏着我原来的问题有种被“什么jQuery Mobile的确实给拦截event.preventDefault链接()方法来不阻止链接的默认操作,即会目标网页?'
在jQuery mobile中使用'document.ready' [不好](http://jquerymobile.com/test/docs/api/events.html)。改为使用'$(document).bind('pageinit',function ...)'。 – vcsjones 2012-08-16 20:22:15
@vcsjones是的,我也读过,但从来不知道如何实现。在一些测试中,我用'$(document).on('pageinit',handler)'替换了'document.ready',但每当我在多页文档中更改页面时,它似乎都会激发。另外,使用'on'代替'pageinit'的'bind'会好吗? – micadelli 2012-08-16 20:35:46